如果在uitableview中取消选中时,如何将颜色设置为选中或选中并重置为上一个颜色的整个单元格?我试过但它显示了一个sec.can的背景颜色任何一个请帮我编码。
答案 0 :(得分:3)
好的,在这里我认为你想要在选择时为你的细胞着色,如果未选择则要另一种颜色,留下那些选定的细胞以前的颜色。所以全局都有一个数组状态。在didSelectRowAtIndexPath中:
if (![myMutableArray containsObject:indexPath]) {
[myMutableArray addObject:indexPath];
}
else{
[myMutableArray removeObject:indexPath];
}
[sampleTableView reloadData];
在cellForRowAtIndexPath中:
if ([myMutableArray containsObject:indexPath]) {
cell.backgroundColor = [UIColor redColor];
}
else{
cell.backgroundColor = [UIColor greenColor];
}
答案 1 :(得分:2)
在你的头文件中创建一个NSIndexPath的对象(让它在这里使用checkedIndexPath)并分配它的属性并合成它。在你的tableView的方法cellForRowAtIndexPath中使用:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
现在在didSelectRowAtIndexPath中使用以下内容:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.backgroundColor = [UIColor green];
}
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
self.checkedIndexPath = indexPath;
}
使用此选项,如果选中,您的单元格将为红色,如果未选中,则为绿色。
答案 2 :(得分:0)
您可以设置UITableViewCell
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell_normal.png"]]
然后在点击事件上,颜色将自动更改。
您可以通过以下方式更改选择的颜色: -
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
用于更改textLabel颜色。
cell.textLabel.textColor = [UIColor blackColor];