0

我正在更改自定义 tableView 按钮颜色上的单元格颜色。单元格 indexRow 在按钮单击时变为绿色,但在滚动后再次变为正常的白色。我尝试了不同的解决方案,但没有结果。你能建议一下吗?谢谢。

-(void)yesAction:(id)sender
{
 _cellButtonPress = YES;
// [myChklist reloadData];
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
UITableView *curTableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [curTableView indexPathForCell:cell];
cell.backgroundColor = [UIColor greenColor];
}
4

1 回答 1

1

当您的单元格滚动时,它会再次绘制。您需要再次设置该属性。

您可以维护一个选定索引的数组。然后在您的 cellForRowAtIndexPath 委托方法中,您可以根据索引将背景颜色设置为白色或绿色。类似于:

  NSMutableArray* indexArray = [[NSMutableArray alloc] init];

在 yesAction 中:

 [indexArray addObject:indexPath];

在 cellForRowAtIndexPath 中:

 if ([indexArray containsObject:indexPath]) {
    cell.backgroundColor = [UIColor greenColor];
}
于 2013-05-06T08:00:39.227 回答