ios - iOS滚动后如何保持tableview的单元格颜色

标签 ios uitableview colors

我正在更改自定义 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/

相关文章:

ios - 按下加号按钮时,在当前单元格下方动态插入新单元格

css - 动态改变颜色的CSS

ios - 自定义选择器可以访问选项值,而不仅仅是选定的索引

ios - 以编程方式快速更改 segue 标识符名称

iphone - 在 Iphone +[UIActivityIndi​​catorView _loadResourcesForStyle :]? 中显示泄漏

java - listview 的 onItemClick 和 case 内的循环

c++ - 为什么 ncurses 的 start_color() 会使常规文本变暗?

iOS:通过应用程序登录 Facebook

ios - 如何排列不同的 UITableViewCell 图像和标签?

ios - 如何设置自调整单元格 iOS8