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

  • SEARCH
  • Home
  • 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 8888929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:08:18+00:00 2026-06-14T22:08:18+00:00

I have a UItableView where every UITableViewCell is containing a UISwitch .Now my question

  • 0

I have a UItableView where every UITableViewCell is containing a UISwitch .Now my question is when i will click in one switch then how can i OFF other switches of the UITableViewCell

In my code i have already made the view and i can ON/OFF the switches.But i want to OFF all other switches except my selected switch.

Please some help me by giving an example or source code example.

With Best Regards

Edit

My Code:

- (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];
        switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
        cell.accessoryView = switchview;
        switchCondition = NO;
        [switchview setOn:NO animated:YES];
        [switchview addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventValueChanged];
        [switchview release];
    }
    if(switchCondition == YES){
    [switchview setOn:YES animated:YES];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",[cellValueArray objectAtIndex:indexPath.row]];
    return cell;
}

- (void)updateSwitchAtIndexPath:(UISwitch*)sender {
    if(sender.on){
        switchCondition = YES;
        [table reloadData];
    }
}
  • 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-06-14T22:08:19+00:00Added an answer on June 14, 2026 at 10:08 pm

    Update your data model used by the table’s data source, then reload the table (or at least the visible rows). This will cause each row to reload and each switch will get updated with the latest data.

    Edit: Here’s an updated version of your code:

    You need an instance variable to track the state of each switch. Create an array to hold the YES and NO values. In the code below I will assume there is an instance variable named switchConditions of type NSMutableArray that has been setup with NSNumber objects representing the YES and NO values for each row. This is similar to your cellValueArray. You should also get rid of your switchView and switchCondition instance variables.

    - (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.selectionStyle = UITableViewCellSelectionStyleNone;
    
            UISwitchView *switch = [[UISwitch alloc] initWithFrame:CGRectZero];
            cell.accessoryView = switch;
            [switchview addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventValueChanged];
            [switch release];
        }
    
        UISwitchView *switch = (UISwitchView *)cell.accessoryView;
        switch.tag = indexPath.row; // This only works if you can't insert or delete rows without a call to reloadData
        BOOL switchState = [switchConditions[indexPath.row] boolValue];
        switch.on = switchState; // this shouldn't be animated
    
        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.textLabel.text = cellValueArray[indexPath.row];
    
        return cell;
    }
    
    - (void)updateSwitchAtIndexPath:(UISwitch*)switch {
        NSInteger row = switch.tag;
        if (switch.on){
            // This switch is on, turn all of the rest off
            for (NSUInteger i = 0; i < switchConditions.count; i++) {
                switchConditions[i] = @NO;
            }
            switchConditions[row] = @YES;
            [self.tableView reloadData];
        } else {
            switchConditions[row] = @YES;
        }
    }
    
    • 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 UItableview where every cell containing 5 cell and these cell are representing
I'm trying to have UITableView with 2 classes of UITableViewCell (section 0 with one
I have a button inside every cell of my UITableView, but when the button
I have UITableView with very large cells with lots of content (more than one
I have a UITableView with regular UITableViewCell, but I don't use any of UITableViewCell's
hii every one i have created a data entry screen like this in which
I have an UITableView in my class with a custom UITableViewCell with an UIImageView
I have a UITableView controller, UITableView xib, and two UITableViewCell subclasses with there xib's.
In my UITableViewCell I have UIImageView which i want to rotate for 180° every

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.