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 9210681
In Process

The Archive Base Latest Questions

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

I have a UITableView with a UISwitch as the accessoryView. My problem is that

  • 0

I have a UITableView with a UISwitch as the accessoryView. My problem is that if I toggle one of the switches then scroll so the switch it out of view it returns to its previous state.

Please see video.

Does anyone know why this might be and how to solve it?

Here is the code that adds the switch view and deals with its action.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"POICell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];

    //set the cell text to the
    cell.textLabel.text = [self.catNames objectAtIndex:[indexPath row]];
    NSString *toggle = [self.toggleArray objectAtIndex:[indexPath row]];
    //add switch
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    //create an instance of the database oject
    DataBase * dataBase = [[DataBase alloc] init];

    //open the database connection
    [dataBase openDB];
    NSString *imageName = [dataBase getPinImageNameFromCatID:[self.catIDs objectAtIndex:[indexPath row]]];

    //get the root file path for the images
    NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString  *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/pinImages/%@",documentsDirectory, imageName];

    //add image
    NSURL *imageURL = [NSURL fileURLWithPath:filePath];
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage *image = [UIImage imageWithData:imageData];

    cell.imageView.image = image;
    NSLog(@"%@",[self.catIDs objectAtIndex:[indexPath row]]);

    UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
    cell.accessoryView = switchView;
    [switchView addTarget:self action:@selector(switchChanged: ) forControlEvents:UIControlEventValueChanged];

    if ([toggle isEqualToString: @"OFF"]) {
        [switchView setOn:NO animated:NO];
    }else{
        [switchView setOn:YES animated:NO];
    }
    return cell;
}

- (void) switchChanged:(id)sender {

    //get the switch that it was sent from
    UISwitch *switchInCell = (UISwitch *)sender;
    //get the cell it was sent from
    UITableViewCell * cell = (UITableViewCell *) switchInCell.superview;
    //get the row it was sent from
    NSIndexPath * indexpath = [self.inputTableView indexPathForCell:cell];
    //cast the indexpath to int
    NSInteger variable = indexpath.row;
    //set the filter as off in the user defualts.
    [self.filterDic setValue:switchInCell.on ? @"ON" : @"OFF" forKey:[self.catIDs objectAtIndex:variable]];
    //store the newdic in the user defualts
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    //save the dic to user defaults
    [prefs setObject:self.filterDic forKey:@"pinFilters"];

}

Thanks for any help.

  • 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-18T01:08:02+00:00Added an answer on June 18, 2026 at 1:08 am

    There are two issues with your code (at least 🙂

    Let me start with the one that is confusing you.
    You set the status of each witch based on toggle. And the value of toggle is tanken from an array self.toggleArray.

    Fine so far.
    But when a value changes and the action switchChanged is called then you update self.filterDic but you do not update self.toggleView.

    And this causes the problem:
    Next time when a cell becomes visible cellForRowAtIndexPath is called again and will set the value based on toggle wich is based on self.toggleArray. And that still has the old values in it … you see?

    You are making this mistake probably because you have not yet fully understood the cell recycle mechanism. And that is probably what causes the second issue that I identified. Let me try to explain.
    iOS or cocoa respectively tries to allocate as view cell objects as nessessary. That means that a cell wich scrolls off the creen is added to a pool from which it can be re-used the next time when a (similar) sell is required. So each time when there is a need for a new cell (one that becomes visible) cellForRowAtIndexPath is called. Wihin that you fetch a cell using dequeueReusableCellWithIdentifier. If there is a cell in that pool that was initialized with the same re-use identfier then that one (or one of those) is returned to the caller.
    In recent iOS (respectively SDK versions) versions a new cell will be allocated and returned if none of these cells exists. (And that is why Murali’s suggestion would not work perfectly either)
    In older versions you had to check cell for nil and alloc/init a new on in those cases.

    After that you freely allocate new subview objects regardless whether the cell was re-cycled and already has those subviews or not. Then you add and add and add the same subviews again and again.

    How can you solve this? There are, as usual, several ways of dealing with that:

    First – Check whether the cell was re-used or not. Just check if the Switch is already there or not. For doing that you could tag it with some value different from 0 and fetch the subview with this tag. If you dont’t get it then the cell is new and you have to create all the additional subvies.

    Second – You could always erase all subviews from the cell right after fetching the cell with dequeueReusableCellWithIdentifier. That is the easiest solultion because you do not have to change mutch to your existing code. It is not the most performant solution though.

    Third – The most elegant solution is probably to subclass UITableViewCell every time when you want to add custom elements to a cell. In that case its init method would be called only once upon creation (and not upon re-usage) and there you can programmatically add all the custom subviews. You can, of course, design the cell and add all subviews in IB as you can with every UIView object. Witin cellForRowAtIndexPath you would only have to care for setting the appropriate values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIViewController, and within that view i have UITableView added in IB
I have a UITableView with cells that contain a UISwitch control. It's similar to
i have a very simple table with only one row that has a uiswitch
I have a UITableView with a UISwitch button in it. I want that when
i got the problem in my apps, i have UITableView and it's scroll slowly,
I have a UITableView with one UISwitch for row. To do it: UISwitch *switchController
I have UITableView.when i click on it's 1 st row one another UITableView opens
I have UITableView with very large cells with lots of content (more than one
I have a UITableView that lists comments from various users. I save all of
I have UITableView...when user tap on row, another screen is opened. The problem is,

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.