我正在更改自定义 tableView 按钮颜色的单元格颜色。单击按钮时单元格索引行变绿,但滚动后它再次变为正常白色。我尝试了不同的解决方案但没有结果。你能建议一下吗?谢谢。
-(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];
}
最佳答案
当你的单元格滚动时,它会再次被绘制。您需要重新设置该属性。
您可以维护选定索引的数组。然后在 cellForRowAtIndexPath 委托(delegate)方法中,您可以根据索引将背景颜色设置为白色或绿色。类似于:
NSMutableArray* indexArray = [[NSMutableArray alloc] init];
在 yesAction 中:
[indexArray addObject:indexPath];
在 cellForRowAtIndexPath 中:
if ([indexArray containsObject:indexPath]) {
cell.backgroundColor = [UIColor greenColor];
}
关于ios - iOS滚动后如何保持tableview的单元格颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16393609/