单击UItableviewcell背景颜色
如何设置一个颜色的select或勾选整个单元格,并重置为以前的颜色,而在uitableview未选中?我试过,但它显示了一个sec.can的背景颜色任何人请帮我编码。
好的,在这里,我想你想在选中时给单元格着色,如果未选中,则要使用另一种颜色,留下那些以前颜色的单元格。 所以有一个arrays在全球范围内。 在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]; }
在你的头文件中创build一个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; }
使用这个选项,如果选中,你的单元格会变成红色,如果未选中,则变成绿色。
你可以设置UITableViewCell
的背景颜色
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell_normal.png"]]
然后点击事件的颜色会自动改变。
您可以通过以下方式更改select的颜色: –
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
用于更改textLabel颜色。
cell.textLabel.textColor = [UIColor blackColor];