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.

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

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?
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.
On your function
On your vvv selector
And you should init the array on your viewDidLoad, lets say for 10 cells