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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:47:11+00:00 2026-05-24T16:47:11+00:00

I have a table where each row represents an object in core data. Each

  • 0

I have a table where each row represents an object in core data. Each row contains a UISwitch (modified to contain the indexPath of the row that contains it) that when toggled should toggle the state property of the object in core data.

What’s currently happening is the switch goes to on when it is touched, and then goes back to off!?

The one clue I have is that when I comment out

// Establish what position the switch should be in
    if([info.state isEqualToString:@"on"]){
        mySwitch.selected = TRUE;
    } else {
        mySwitch.selected = FALSE;
    }

and the two info.state lines in the changeState method, the switch works fine. It seems like the info.state setter, regardless of their bearing on the switch state, prevent the switch from switching.

Am I just being colossally negligent and not managing memory correctly?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    InfoObject *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = info.name;

    // Use a custom UISwitch that holds a pointer to the row it is associated with
    NamedUISwitch *mySwitch = [[[NamedUISwitch alloc] initWithFrame:CGRectZero] autorelease];
    mySwitch.indexPath = indexPath;
    [mySwitch addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventValueChanged];
    // And of course we need to know what the state is
    NSLog(@"switchState:%@",info.state);

    // Establish what position the switch should be in
    if([info.state isEqualToString:@"on"]){
        mySwitch.selected = TRUE;
    } else {
        mySwitch.selected = FALSE;
    }

    cell.accessoryView = mySwitch;
}

- (void) changeState:(id)sender {
    InfoObject *info = [self.fetchedResultsController objectAtIndexPath:((NamedUISwitch*)sender).indexPath];

    if(((NamedUISwitch*)sender).on){
        info.state = @"on";
    } else {
        info.state = @"off";
    }

    NSError *error;
    if (![self.context save:&error]) {
        NSLog(@"Whoops, couldn't save state: %@", [error localizedDescription]);
        //TODO: alertview
    }
}
  • 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-24T16:47:11+00:00Added an answer on May 24, 2026 at 4:47 pm

    I think that code should be:

    // Establish what position the switch should be in
        if([info.state isEqualToString:@"on"]){
            mySwitch.on = YES;
        } else {
            mySwitch.on = NO;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a databases table with ~50K rows in it, each row represents a
I have an object that has X number of fields. Each row in my
I have a table with multiple rows. Each row is a form. I want
I have a table style page with rows. Each row has a checkbox. I
I have a million rows in a database table. For each row I have
I have a table that stores all the volunteers, and each volunteer will be
I have a table that has about 1/2 million records in it. Each month
I have a table with each cell containing a label and a text field.
I have a table of items, each of which has a date associated with
I have a table with a Date column. Each Date may appear multiple times.

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.