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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:29:15+00:00 2026-05-23T00:29:15+00:00

I am trying to create a ‘blank_star icon’ in every cell of a table

  • 0

I am trying to create a ‘blank_star icon’ in every cell of a table view, the star should become a ‘solid’ star after the user clicks on it.

I have created a subclass of UIButton as seen below

//.h file
@interface Bleh : UIButton {

}

+(id)specialInit;
-(void)vvv;

@end

//.m file
@implementation Bleh

+(id) specialInit
{
    Bleh* button=[super buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"blank_star.png"] forState:UIControlStateNormal];    
    [button setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateDisabled];    
    [button addTarget:button action:@selector(vvv)    forControlEvents:UIControlEventTouchUpInside];    
    NSLog(@"%d",[button isEnabled]);
    return button;
}


-(void)vvv
{
    NSLog(@"button tapped");
    [self setEnabled:false];
}

@end

I added the subclass of UIButton in my table view’s cellforRow: method as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    int row = indexPath.row;
    NSString *cc = [array objectAtIndex:row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        // Configure the cell...    
        Bleh *button = [Bleh specialInit];
        button.frame = CGRectMake(0, 0, 100, 100);
        NSLog(@"Button:%@ at row number: %i",button, indexPath.row);

        cell.textLabel.text = cc;
        [cell.contentView addSubview:button];

    }
    return cell;
}

However I am getting an issue when running the app. For instance, if I click on the cell marked ‘a’, the star becomes solid as expected.

enter image description here

The strange thing is that after scrolling down, I see some other cells with the solid star as well (see cell ‘e’).

enter image description here

Can anyone help to explain why this is happening? It seems like the state of the cell is being re-used in other cells. How can I avoid this happening?

  • 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-23T00:29:16+00:00Added an answer on May 23, 2026 at 12:29 am

    You can store the state of the button in a NSMutableArray and when you draw the cell you set if it is enabled or disabled based on the NSMutableArray. To change the value on the Array you should Tag the cell and make the change on your vvv selector.

    //.h file
    @interface Bleh : UIButton {
        NSMutableArray *data;
    }
    

    On your function

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ...
        // Configure the cell...    
        Bleh *button = [Bleh specialInit];
        [button setTag:row] // row is the id of the button
        if([data objectAtIndex:row] isEqualToString:@"ENABLED"]) [button setEnabled:TRUE];
        else [button setEnabled:FALSE];
        ...
    }
    

    On your vvv selector

    -(void)vvv:(id)sender {
        if([sender isEnabled]) {
            [sender setEnabled:FALSE];
            [data replaceObjectAtIndex:[sender tag] withObject:@"DISABLED"];
        }
        else {
            [sender setEnabled:TRUE];
            [data replaceObjectAtIndex:[sender tag] withObject:@"ENABLED"];
    
        }
    }
    

    And you should init the array on your viewDidLoad, lets say for 10 cells

    - (void)viewDidLoad
    {
        ...
        data = [[NSMutableArray alloc] initWithCapacity:10];
        for ( int i = 0; i < 10; i++ ) {
            [data addObject:@"ENABLED"];
        }
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create an Oracle trigger that runs after a table is updated in
Trying to create a black line in my view to separate text blocks but
I'm trying create a scaling layout for an Android application. The whole screen should
I am trying create a 'live' view where our clients can edit the colors
I am trying create a table and add this table into tablecell (table inside
I am trying create custom namespaces after reading the article at Custom Namespaces I
I am trying create a layout. There are several inner layouts that should like
I am trying create a search word puzzle box with JQuery. Basically a table
I'm trying to create a Temp Table that has a key field that auto
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can

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.