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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:49:32+00:00 2026-06-17T11:49:32+00:00

I am attempting to create a checkbox in each uitableviewcell that can toggle between

  • 0

I am attempting to create a checkbox in each uitableviewcell that can toggle between selected and unselected. To make sure that a single selection doesn’t recycle when loading new cells, I created a mutable array of strings to signify “Uncheck” or “Check” (i.e. when the button image should appear unchecked or checked). When the check button is pressed, the string is replaced by “Check” or “Uncheck”. For some reason that I cannot see, when the tableview scrolls and the cell with the selected check button goes offscreen, the array loses the change made to it, and, thus, the check string is lost.

Any ideas what’s wrong?

Thanks in advance for the help.

An NSLog of selectedCheckArray right after button in 1st cell is checked in “checkButtonClicked”:
{ Check, Uncheck, Uncheck, Uncheck, Uncheck, Uncheck, Uncheck }

An NSLog of selectedCheckArray after I scroll down so that 1st cell is no longer on screen:
{ Uncheck, Uncheck, Uncheck, Uncheck, Uncheck, Uncheck, Uncheck }

Here’s the code:

.h

    @property (strong, nonatomic) NSMutableArray *selectedCheckArray;

.m

    @synthesize selectedCheckArray;

…

    - (void)viewWillAppear:(BOOL)animated
    {
        // cellDataArray loaded here

        selectedCheckArray = [[NSMutableArray alloc] init];

        for (int i = 0; i<[cellDataArray count]; i++) {
            [selectedCheckArray addObject:@"Uncheck"];
        } 

        [super viewWillAppear:animated];
    } 


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [cellDataArray count];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         static NSString *CellIdentifier = @"choiceCell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        UIButton *checkButton = (UIButton *)[cell viewWithTag:1];

        if ([[selectedCheckArray objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])         {
            [checkButton setImage:[UIImage imageNamed:@"unchecked"]   
               forState:UIControlStateNormal];
        } else {
            [checkButton setImage:[UIImage imageNamed:@"checked"] 
               forState:UIControlStateNormal];
        }
        [checkButton addTarget:self action:@selector(checkButtonClicked:)  
           forControlEvents:UIControlEventTouchUpInside];

        return cell;
    }

…

    - (void)checkButtonClicked:(id)sender
    {
        // indexPath of cell of clicked button
        CGPoint touchPoint = [sender convertPoint:CGPointZero toView:choiceTable];
        NSIndexPath *indexPath = [choiceTable indexPathForRowAtPoint:touchPoint];

        // Not using tag as sender will keep reference of clicked button
        UIButton *button = (UIButton *)sender;

        //Checking the condition button is checked or unchecked.
        //accordingly replace the array object and change the button image
        if([[selectedCheckArray objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
        {
            [button setImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal];
            [selectedCheckArray replaceObjectAtIndex:indexPath.row withObject:@"Check"];
        } else {
            [button setImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
            [selectedCheckArray replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];
        }
    }
  • 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-17T11:49:33+00:00Added an answer on June 17, 2026 at 11:49 am

    Try this,

    in .h file,

    NSMutableArray *arrayCellChecked;
    
    @property(nonatomic, retain) NSIndexPath *lastIndexPath;
    

    in .m file

    @synthesize lastIndexPath;
    int oldRow, newRow;
    

    In DidLoad Method,

    arrayCellChecked = [[NSMutableArray alloc] init];
    

    Table Row Method,

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
            //Your Code
            ----------
    
            newRow = [indexPath row];
            oldRow = [[self lastIndexPath] row];
    
    
            cell.accessibilityViewIsModal = (newRow == oldRow && lastIndexPath != nil) ? NO : YES;
    
            if(cell.accessibilityViewIsModal == YES)
                [checkButton setImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
            else
                [checkButton setImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal];
    
            //Your Method for Button Click
            -----------
    
            return cell;
    }
    

    Table Cell Selection Method,

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        //Your Cell Code
        -------
    
        if (cell.accessibilityViewIsModal == NO) {
    
            cell.imgCheck.image = [UIImage imageNamed:@"checked"];
            cell.accessibilityViewIsModal = YES;
    
            NSString *cellValue = yourArray.object;
    
            [arrayCellChecked addObject:cellValue];
    
            -------
        }
    
        else{
    
            cell.imgCheck.image = [UIImage imageNamed:@"unchecked"];
            cell.accessibilityViewIsModal = NO;
    
            NSString *cellValue = yourArray.object;
    
            [arrayCellChecked removeObject:cellValue];
    
            --------
        }
     }
    

    It’ll done for ‘didSelectRowAtIndex’.. No need of button click method.

    I’ve resolved it with this way.

    Hopefully It’ll work for you.
    Thanks.

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

Sidebar

Related Questions

I'm attempting to create a single Controller class to handle all foreseeable surveys that
I'm attempting to create a function where I can pass names and values that
I'm attempting to create an xquery expression that will return selected nodes but will
I am attempting to create a report, utilizing a matrix, that only displays columns
I'm attempting to create a class and inherit from Toplevel() so that the GUI
I'm attempting to create a directive that binds specific keypresses to functions specified in
I'm attempting to create an array of strings that represent the directories stored in
I'm attempting to create an ASP.NET/C# page that runs a PowerShell script that will
I am attempting to create a report that contains a list nested within another
I am attempting to create a custom server control CollapsablePanel that extends ASP.net's Panel.

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.