繁体   English   中英

单击时的UItableViewCell背景颜色

[英]UItableviewcell background color while click

我如何尝试在uitableview中未选中的情况下为选中或选中的整个单元格设置颜色并重置为以前的颜色?我试过了,但它显示了背景颜色一秒钟。任何人都可以帮我编码。

好的,在这里我想您要为单元格选择颜色,而如果未选择,则要使用另一种颜色,使那些选择的单元格具有以前的颜色。 因此,全局具有数组状态。 在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];
}

在您的头文件中创建一个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"]]

然后在单击事件中颜色将自动更改。

您可以通过以下方式更改选择的颜色:-

cell.selectionStyle = UITableViewCellSelectionStyleBlue;

用于更改textLabel颜色。

cell.textLabel.textColor = [UIColor blackColor];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM