【发布时间】:2012-08-11 10:29:35
【问题描述】:
如何在 uitableview 中未选中时为选择或选中标记并重置为以前颜色的整个单元格设置颜色?我尝试过,但它显示背景颜色一秒。任何人都可以帮我编码。
【问题讨论】:
标签: iphone ios uitableview uibackgroundcolor
如何在 uitableview 中未选中时为选择或选中标记并重置为以前颜色的整个单元格设置颜色?我尝试过,但它显示背景颜色一秒。任何人都可以帮我编码。
【问题讨论】:
标签: iphone ios uitableview uibackgroundcolor
好的,我认为您想在选中时为单元格着色,如果未选中则为另一种颜色,将那些选定的单元格保留为以前的颜色。所以在全球范围内有一个数组状态。在 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 的对象(让它在这里使用的检查索引路径)并分配它的属性并合成它。在你的 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];
【讨论】: