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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:38:33+00:00 2026-05-27T18:38:33+00:00

When i checked a cell, the image associated is changed, but when i had

  • 0

When i checked a cell, the image associated is changed, but when i had scrolled down and up, the cell got its default image this is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    NSArray *listData =[self->tableContents objectForKey:
                        [self->sortedKeys objectAtIndex:[indexPath section]]];
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
    if(cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:SimpleTableIdentifier];
    }
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];
    UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]];
UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]];

BOOL bChecked = NO;//By default, it's unchecked, so NO

/// Assign cell according to `bChecked`
cell.accessoryView = (bChecked ? checked : unchecked); 
return cell;

}

EDIT:

I have edited my code to look as above, but i still get the issue, the default image is getting back when i scroll up my table view.

EDIT 2:
@Openside: So according to your approach, my snippet should look like this:

- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    NSArray *listData =[self->tableContents objectForKey:
                        [self->sortedKeys objectAtIndex:[indexPath section]]];

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
    if(cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:SimpleTableIdentifier];
    }
    NSUInteger row = [indexPath row];
    textClass *myText = (textClass*)[listData objectAtIndex:row];
    cell.textLabel.text = myText.text;
    UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]];
    UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"checked.png"]];



    cell.accessoryView = (myText.isChecked ? checked : unchecked); 
    return cell;
}


- (void)tableView:(UITableView *)tableView

didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    textClass *myText=[[textClass alloc]init];
    myText.isChecked=YES;
}

I got this exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString text]: unrecognized selector sent to instance 0x673bc'

The line indicating the exception issue is this (in cellForRowAtIndexPath delegate method):

cell.textLabel.text = myText.text;

I have added the class textClass although i am not so convinced, i think i can use a property in the holder class of my UITableView.

  • 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-27T18:38:33+00:00Added an answer on May 27, 2026 at 6:38 pm

    The problem is that the cell will be refreshed (or reused hence the dequeueReusableCellWithIdentifier ) when you scroll it out of view and then back into view. You need to have a flag on your source object to say if it’s checked or not and then add an if statement to determine if the checked.png or unchecked.png image is used.

    However, looking at your code the source object is just text, it may be worth creating a subclassed NSObject with two properties

    text and
    checked

    Then your cellForRowAtIndexPath code can determine which image to show.

    I hope that helps.

    - (UITableViewCell *)tableView:(UITableView *)tableView
    
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
    if(cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:SimpleTableIdentifier];
    }
    NSUInteger row = [indexPath row];
    textClass *myText = (textClass*)[listData objectAtIndex:row];
    cell.textLabel.text = myText.text;
    UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]];
    UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"checked.png"]];
    
    
    
    /// Assign cell according to `bChecked`
    cell.accessoryView = (myText.isChecked ? checked : unchecked); 
    return cell;
    
    }
    
    @interface textClass : NSObject {
    
    @private
        NSString *_text;
        BOOL _isChecked;
    }
    
    @property (nonatomic, retain) NSString *text;
    @property (nonatomic, assign) BOOL isChecked;
    
    @end
    
    @implementation textClass
    
    @synthesize text = _text;
    @synthesize isChecked = _isChecked;
    
    @end
    

    Ok so here I have created a class called textClass it has two properties text and isChecked. When you load your view populate an NSMutableArray (listData) with these objects using the text you were previously using. When your cell gets checked set the isChecked property to YES when the cell gets reused this property has retained its state outside of the delegate method and should render correctly.

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

Sidebar

Related Questions

I want checked more cell with one click(touch). In this code I try to
I checked out the three20 source and was trying to follow this guide to
I checked all the topics, but i simply don't know why my script does
This an incredibly annoying bug. I had a imageView and 2 Labels in a
Table view cell imageView is working on the simulator but not on the device..
i have a problem on below image: I have detailsview, but i can't show
I don't know why this is happening, but I get this error: -[__NSArrayM section]:
Here is the code: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier
The cell contains nothing but a checkbox. It is rather wide because of text
I've created a custom UITableViewCell but am having trouble updating the contents of the

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.