Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7562609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:27:02+00:00 2026-05-30T13:27:02+00:00

I have a UITableView with one UISwitch for row. To do it: UISwitch *switchController

  • 0

I have a UITableView with one UISwitch for row. To do it:

UISwitch *switchController = [[UISwitch alloc] initWithFrame:CGRectZero];
if([valor isEqualToString:@"true"])  
    [switchController setOn:YES animated:YES];
else
    [switchController setOn:NO animated:YES];

switchController.tag = row;
[switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switchController;
[switchController release];

Ok, i want exclusive row, when i touch one UISwitch i need put all others UISwitch in OFF.
But i can´t do it…Someone can help me?

Thanks friends.

-(void) switchChanged:(id)sender{
    UISwitch *switchController = sender;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:27:04+00:00Added an answer on May 30, 2026 at 1:27 pm

    One simple approach could be like this.

    You want that user can switch on only one row at a time. For this you will need to keep track of two things first the switches(we can access it via cell’s accessory view) and second the switch which is on.(for this suppose we use tag property).

    So we will need:
    >One variable to keep track of which row's switch is on.
    >Array which will hold the of the switches.
    

    I have created a sample app with following code:(I am taking number of rows as constant to 10)

    Code

    SwitchTableController.h

        #import <UIKit/UIKit.h>
    #define NUMBER_OF_ROWS 10
            @interface SwitchTableController : UITableViewController {
    
                int selectedSwitchRow;
                NSMutableArray *switchArray;
            }
    
            @end
    

    SwitchTableController.m code

    #import "SwitchTableController.h"
    
    @implementation SwitchTableController
    
    #pragma mark -
    #pragma mark View lifecycle
    
    - (void)dealloc {
        [switchArray release];
        [super dealloc];
    
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        selectedSwitchRow = -1;
        if (switchArray == nil)
        {
            switchArray  = [[NSMutableArray alloc] initWithCapacity:10];
        }
        for (int i=0; i<NUMBER_OF_ROWS; i++)
        {
            UISwitch *switchController = [[UISwitch alloc] initWithFrame:CGRectZero];
            [switchController setOn:NO animated:YES];
            switchController.tag = i;
            [switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
            [switchArray addObject:switchController];
            [switchController release];
        }
    }
    
    #pragma mark -
    #pragma mark Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return NUMBER_OF_ROWS;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        cell.accessoryView = [switchArray objectAtIndex:indexPath.row];
       return cell;
    }
    
    -(void) switchChanged:(UISwitch *)sender
    {
        if (selectedSwitchRow >= 0 && selectedSwitchRow<[switchArray count] && selectedSwitchRow != sender.tag)
        {
            UISwitch *tempSwitch = [switchArray objectAtIndex:selectedSwitchRow];
            [tempSwitch setOn:NO animated:YES];
            [self.tableView reloadData];
        }
        selectedSwitchRow = sender.tag;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have UITableView.when i click on it's 1 st row one another UITableView opens
i have a UITableView where i want one cell(database row) needs to be default,
I have two UITableview's and if i select a one row in each table,
i have a very simple table with only one row that has a uiswitch
I have a UITableView for different datas. When the user select one row, then
I have a UITableView With Two Sections. First section has one row and the
I have two UITableview. The first one shows 3 sections with 3 row each
I have UITableView with very large cells with lots of content (more than one
Does any one have tried to customize default section index displayed in UITableView. I
So I have a UIWebView implemented in one .m file and a UITableView implemented

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.