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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:09:40+00:00 2026-05-26T04:09:40+00:00

I have a UITableView in which you can only tick 4 cells. If I

  • 0

I have a UITableView in which you can only tick 4 cells. If I tick a certain amount of cells and then scroll up or down, it will tick random cells. I don’t understand the logic behind this because the only method I use for ticking is DidSelectRowAtIndex which doesn’t get executed (I put a breakpoint in there to check it). I really do not know what could be causing this but here is cellForRowAtIndex:

- (UITableViewCell *) tableView: (UITableView *) tableView
          cellForRowAtIndexPath: (NSIndexPath *) indexPath
{

        static NSString *cellName = @"PrefsTableViewCell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                                 cellName];
        if (cell == nil)
        {
                cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName] autorelease];
        }

        NSUInteger row = [indexPath row];
        Drink *drink = [drinks objectAtIndex:row];

        cell.textLabel.text = drink.name;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", drink.caffeine];
        [[cell imageView] setImage:drink.image];

        return cell;
}

I’m having a feeling that it has something to do with this but I can’t really tell.

This is what it looks like prior to scrolling:

enter image description here

This is what happens when I scroll up to the top:

enter image description here

I did not click that top one. If I scroll down and up again it’s likely to get unticked and another one will get ticked.

It may be important so here is the didSelectRowAtIndexPath method:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        UITableViewCell *cell = [tableView cellForRowAtIndexPath: indexPath];

        NSLog(@"TESTING");

        if ((cell.accessoryType == UITableViewCellAccessoryNone) && [[DataContainerSingleton theDataContainerSingleton].favourites count] < 4)
        {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;

                [[DataContainerSingleton theDataContainerSingleton].favourites addObject:[drinks objectAtIndex:indexPath.row]];
                NSLog(@"CHECKED %d", [[DataContainerSingleton theDataContainerSingleton].favourites count]);
        }
        else if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
            for (int i = 0; i < [[DataContainerSingleton theDataContainerSingleton].favourites count]; i++)
            {
                NSString *drinkName = [[drinks objectAtIndex:indexPath.row] name];
                NSString *favName = [[[DataContainerSingleton theDataContainerSingleton].favourites objectAtIndex:i] name];

                if ([drinkName isEqualToString: favName])
                {
                    cell.accessoryType = UITableViewCellAccessoryNone;
                    [[DataContainerSingleton theDataContainerSingleton].favourites removeObjectAtIndex:i];
                    NSLog(@"UNCHECKED %d", [[DataContainerSingleton theDataContainerSingleton].favourites count]);
                }
            }

        }
}
  • 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-26T04:09:41+00:00Added an answer on May 26, 2026 at 4:09 am

    You are not saving which items have been ticked. You should save the indexes in an array, and set the tick in cellForRowAtIndexPath:. Something like-

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
            if(![self.tickedIndexPaths containsObject:indexPath]) //tickedIndexPaths is an array
                [self.tickedIndexPaths addObject:indexPath];
            else
                [self.tickedIndexPaths removeObject:indexPath];
    }
    

    Then-

    - (UITableViewCell *) tableView: (UITableView *) tableView
              cellForRowAtIndexPath: (NSIndexPath *) indexPath
    {
    
            static NSString *cellName = @"PrefsTableViewCell";
    
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                                     cellName];
            if (cell == nil)
            {
                    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName] autorelease];
            }
    
            NSUInteger row = [indexPath row];
            Drink *drink = [drinks objectAtIndex:row];
    
            cell.textLabel.text = drink.name;
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", drink.caffeine];
            [[cell imageView] setImage:drink.image];
    
            if([self.tickedIndexPaths containsObject:indexPath])
            {
                  cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            else
            {
                  cell.accessoryType = UITableViewCellAccessoryNone;
            }
    
            return cell;
    }
    

    Keep in mind that due to the reuse of cells, the last else block is very important.

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

Sidebar

Related Questions

I have a UITableView which contains x amount of cells. However, I want to
I have a UITableView with 3 empty rows. The user can select which text
I have a UITableView which displays about 5 cells at a time, yet in
I have a grouped UITableView which displays a number of cells, which includes both
I have a UITableView which sometimes has only a few rows. I would like
I have a UITableView with 8 cells(sections) in total in which 6 of them
I have a button (a UINavigationBarItem) used for editing my UITableView, which only allows
I have a UITableView which is populated by an array, I have a button
I have a UITableView which is backed by a NSFetchedResultsController . I would like
I have a UITableView which I want to populate with details from an array

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.