I created a custom UITableViewCell in IB. I have a custom UIButton called CheckMarkButton that I added in IB and has a background image of “unchecked.png” (empty box with no checkmark).
When I use this cell in the TableView, in the cellForRowAtIndexPath, I add a target for the button:
cell.CheckMarkButton addTarget:self action@selector(CheckmarkButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
Then for the selector:
- (void)CheckmarkButtonPressed:(id)sender {
UIButton *theButton = (UIButton *)sender;
theButton.selected = !theButton.selected;
[theButton setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
}
So this part works as expected. Now I have another method called from the section header where I check or uncheck all the TableView rows in that section. After I get access to the cell, I try to set the button to selected to show that it’s checked
cell.CheckMarkButton.selected = YES;
With this statement, I do not see the checkmark icon change in my cell. What am I doing wrong? Thanks.
You’re not changing the background, just setting the enabled property.
But in general, you’re just doing this wrong. Rather than manually changing the image whenever the button is selected, you should configure different images for each state in Interface Builder. This way whenever you mark the button as selected the image will update automatically.