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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:07:59+00:00 2026-06-06T10:07:59+00:00

I have a table view with a custom cell ( UISwitch on every cell

  • 0

I have a table view with a custom cell (UISwitch on every cell of the tableview).

I have set my cell like this:

switchView = [[UISwitch alloc] initWithFrame:CGRectMake(200.0f, 5.0f, 75.0f, 30.0f)];
cell.accessoryView = switchView;
[switchView setOn:NO animated:NO];
[switchView addTarget:self action:@selector(favorite:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:switchView];

And the action that is called by the user when he changes the UISwitch is:

-(IBAction)favorite:(id)sender 
{
    indexPath = [self.tableView indexPathForCell:(UITableViewCell*)[sender superview]];
    NSMutableArray *favoriteList = [[NSMutableArray alloc]init];
    NSString *favoriteItem = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
    NSLog(favoriteItem);

    if ([switchView isOn]) 
    {
        [favoriteList addObject:favoriteItem];

        NSUserDefaults *favoriteDefaults = [NSUserDefaults standardUserDefaults];
        [favoriteDefaults setObject:favoriteList forKey:@"MyFavorites"];
        NSLog(@"%@", favoriteList);
    }
    else
    {
        [favoriteList removeObject:favoriteItem];

        NSUserDefaults *favoriteDefaults = [NSUserDefaults standardUserDefaults];
        [favoriteDefaults setObject:favoriteList forKey:@"MyFavorites"];
        NSLog(@"%@", favoriteList);
    }
}

The problem is:
When testing the app, just the last item (cell) of the table view works properly.

For the others, the debugger returns that the state of the UISwitch is always “OFF”.

What is the problem?

  • 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-06T10:08:02+00:00Added an answer on June 6, 2026 at 10:08 am

    A couple of immediate reactions:

    1. In (IBAction)favorite:(id)sender you are not grabbing the switchView for the cell in question, but rather just using what was used by the last reference of that class ivar. Likewise, in your cellForRowAtIndexPath, you appear to be setting (and repeatedly resetting) this same, single switchView ivar. Probably shouldn’t be an ivar at all. You could make switchView a local variable of cellForRowAtIndexPath. You could also make it a local variable of favorite. But most importantly, favorite should setting it as UISwitch *switchView = sender.

    2. I presume you want to save all of your favorites to user defaults. Your current (IBAction)favorite:(id)sender will recreate favoriteList each time, and it will have either one item in it or no items in it, but certainly discarding any other favorites you may have selected in the past.

    3. A minor point, but if you set the accessory cell, you don’t need to also add the switchView to the subview.

    So, I’d suggest modifying cellForRowAtIndexPath as follows:

    NSString *title = [_favorites objectAtIndex:indexPath.row]; // clearly, you'll get the text however you're getting your text now ... I'm just storing in array
    cell.textLabel.text = title;
    
    BOOL isFav = [self isFavorite:title]; // go to user defaults and find out if it's there or not
    
    UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(200.0f, 5.0f, 75.0f, 30.0f)];
    [switchView setOn:isFav animated:NO];
    [switchView addTarget:self action:@selector(toggleFavoriteSwitch:) forControlEvents:UIControlEventValueChanged];
    cell.accessoryView = switchView;
    

    This uses isFavorite:

    - (BOOL)isFavorite:(NSString *)title
    {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSArray *listOfFavorites = [userDefaults objectForKey:kMyFavsKey];
    
        if (listOfFavorites)
            return [listOfFavorites containsObject:title];
    
        return NO;
    }
    

    Then, obviously, you need to define your toggleFavoriteSwitch (previously called favorite) method:

    - (IBAction)toggleFavoriteSwitch:(UISwitch *)switchView
    {
        UITableViewCell *cell = (UITableViewCell *)[switchView superview];
    
        [self updateFavoriteInUserDefaultsFor:cell.textLabel.text withValue:[switchView isOn]];
    }
    

    And this is updating user defaults as follows:

    - (void)updateFavoriteInUserDefaultsFor:(NSString *)title withValue:(BOOL)isOn
    {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
        NSArray *oldFavorites = [userDefaults objectForKey:kMyFavsKey];
    
        NSMutableArray *newFavorites;
    
        if (!oldFavorites)
            newFavorites = [[NSMutableArray alloc] init];
        else
            newFavorites = [NSMutableArray arrayWithArray:oldFavorites];
    
        if (isOn)
            [newFavorites addObject:title];
        else
            [newFavorites removeObject:title];
    
        [userDefaults setObject:newFavorites forKey:kMyFavsKey];
    
        [userDefaults synchronize];
    }
    

    Hopefully this remedies your issue.

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

Sidebar

Related Questions

I have a custom view in a custom table cell. Every time a specific
I have a table view that has custom cells. In every cell I have
I have a custom tableview cell created programmatically that consists of: a background view
I have a table cell for which I set its contentView to a custom
I have a table view with a custom cell (governed by a custom class
I have a table view where in I have loaded my custom cell. I
I have custom table view cell with images (loaded from app document directory), labels,
I have a custom Table View Cell that displays the details of a Conference
I have an table view where i am loaded custom cell from different nib,
I have a table view of custom cells representing individual uploads and each cell

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.