我将自定义颜色放入可根据情况改变的表格单元格中。下面的代码可以很好地来回改变颜色。我不喜欢每次都必须执行 reloadData
才能显示颜色的事实。是否有更便宜的方法来强制更新单元格颜色?
这里还有其他代码被剥离了。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// testing for old selected color
if (checkedIndexPath)
{
// returning old checked cell back to blue
UITableViewCell *uncheckCell = [tableView cellForRowAtIndexPath:checkedIndexPath];
uncheckCell.backgroundColor = [UIColor blueColor];
}
// changing selected cell background color
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor greenColor];
checkedIndexPath = indexPath;
// reloadingtable data
[self.tableVerseView reloadData];
}
最佳答案
您不需要 reloadData 方法。有 reloadRowsAtIndexPaths 方法:
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[(MyCellClass *)[self.tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor greenColor]];
[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];
}
关于iphone - 在没有重新加载数据的情况下强制在表中进行屏幕更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15175961/