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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:30:28+00:00 2026-05-29T21:30:28+00:00

Sorry I’m pretty new to iOS dev. I have a UITableView setup from cells

  • 0

Sorry I’m pretty new to iOS dev.

I have a UITableView setup from cells being pulled from a single XiB nib. I’ve created a on/off switch in the nib, and I am trying to save the state of the switch upon viewWillDisappear for the number of cells that I have. (6 cells to be exact).

How can I loop through all the cells and save this information?

I tried this in my UIViewController to get the info for one cell:

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    UITableView *tv = (UITableView *)self.view;
    UITableViewCell *tvc = [tv cellForRowAtIndexPath:0];

}

it gives me the error “Program received signal: “EXC_BAD_INSTRUCTION”.

How can I accomplish this?

  • 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-29T21:30:32+00:00Added an answer on May 29, 2026 at 9:30 pm

    You have to pass a valid NSIndexPath to cellForRowAtIndexPath:. You used 0, which means no indexPath.

    You should use something like this:

    UITableViewCell *tvc = [tv cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    

    BUT. Don’t do this. Don’t save state in the UITableViewCell.
    Update your dataSource when a switch changed its state.

    If you have implemented the UITableViewDataSource methods the right why your tableView reuses cells. That means the state of your cells will vanish when the cells are reused.

    Your approach might work for 6 cells. But it will fail for 9 cells.
    It will probably even fail if you scroll the first cell off screen.


    I wrote a quick demo (if you don’t use ARC add release where they are necessary) to show you how you should do it instead:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.dataSource = [NSMutableArray arrayWithCapacity:6];
        for (NSInteger i = 0; i < 6; i++) {
            [self.dataSource addObject:[NSNumber numberWithBool:YES]];
        }
    }
    
    - (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];
            UISwitch *aSwitch = [[UISwitch alloc] init];
            [aSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
            cell.accessoryView = aSwitch;
        }
        UISwitch *aSwitch = (UISwitch *)cell.accessoryView;
        aSwitch.on = [[self.dataSource objectAtIndex:indexPath.row] boolValue];
        /* configure cell */
        return cell;
    }
    
    - (IBAction)switchChanged:(UISwitch *)sender 
    {
    //    UITableViewCell *cell = (UITableViewCell *)[sender superview];
    //    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        CGPoint senderOriginInTableView = [sender convertPoint:CGPointZero toView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:senderOriginInTableView];
        [self.dataSource replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:sender.on]];
    }
    

    as you see it’s not very complicated to not store state in the cells 🙂

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

Sidebar

Related Questions

sorry, I am pretty new in WPF and I think it's pretty easy, but
Sorry, guys.I am quite new in mysql but I do need help from getting
Sorry, this is along one to document.. OLD SETUP I have a J2ME client
Sorry this is basic, but I'm new to Python and Django. I have a
Sorry for being verbose... I have some existing maven projects. I imported them into
Sorry for the basic question - I'm a .NET developer and don't have much
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry, I'm new to SVN and I looked around a little for this. How
Sorry the title isn't more help. I have a database of media-file URLs that
(sorry about the long title) I have a custom object Person, which in turn

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.