0
votes

When I reload my tableView, my custom cell button does not reload to its initial state. I would like to have the checks return to the "plusImage". Any idea why this isnt working? Does reloadData envoke new reused cells?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ReusableIdentifier = @"Cell";
    SetListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReusableIdentifier forIndexPath:indexPath];


    cell.delegate = self;



            UIImage *checkImage = [UIImage imageNamed:@"check.png"];
            UIImage *plusImage  = [UIImage imageNamed:@"plusButton"];



            if ([self.selectedRows containsIndex:indexPath.row]) {
                [cell.plusButton setBackgroundImage:checkImage forState:UIControlStateNormal];
            }
            else {
                [cell.plusButton setBackgroundImage:plusImage forState:UIControlStateNormal];
            }
            cell.tag = indexPath.row;





    return cell;
}

when the cell is prepareForReuse

-(void)prepareForReuse
{
    UIImage *plusImage = [UIImage imageNamed:@"plusImage"];
    [self.plusButton setBackgroundImage:plusImage forState:UIControlStateNormal];
    self.plusButton.enabled = YES;

    [super prepareForReuse];
}
1
There's an extra } after cell.tag = ... . Would you please paste the complete code? - Armin
I have a lot of code, it would get very messy. this is everything I am using though. @ArminM - Andy
For performance reasons you should reset the cells content within cellForRowAtIndexPath: and not prepareForReuse. - Jonathan
I have a much better way for you to implement this. Check my answer below. :) - Armin

1 Answers

0
votes

Here's how you should use a UIButton's states:

In interface builder or inside your custom class (in this case: SetListTableViewCell, although I suggest changing that name) you should set the button's background images for different states: UIControlStateNormal & UIControlStateHighlighted. Then set the button's state to Normal or Highlighted instead. So in SetListTableViewCell.m:

- (void)awakeFromNib
{
    [super awakeFromNib];

    [self.plusButton setBackgroundImage:[UIImage imageNamed:@"plusImage"] forState:UIControlStateNormal];
    [self.plusButton setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateHighlighted];

}

-(void)prepareForReuse
{
    [super prepareForReuse];

    self.plusButton.highlighted = NO;
}

and then:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ReusableIdentifier = @"Cell";
    SetListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReusableIdentifier forIndexPath:indexPath];

    cell.delegate = self;
    [cell.plusButton setHighlighted: [self.selectedRows containsIndex:indexPath.row]];
    cell.tag = indexPath.row;

    return cell;
}

If you're using TableView's selection, then you can even use:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    self.plusButton.highlighted = selected;
}