gpt4 book ai didi

iOS:背景颜色——UITableView 和 Accessory 具有相同的背景颜色

转载 作者:可可西里 更新时间:2023-11-01 04:22:38 24 4
gpt4 key购买 nike

我有一个 UISearchBar。当我选择单元格时,我希望整个单元格都具有 [UIColor grayColor];

通过下面的代码,contentView 颜色显示为灰色;但是,背景 accessoryType 颜色显示为蓝色:

enter image description here

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     

UITableViewCell *cell = [self.searchDisplayController.searchResultsTableView cellForRowAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor grayColor];

if (self.lastSelected && (self.lastSelected.row == indexPath.row))
{
cell.accessoryType = UITableViewCellAccessoryNone;
[cell setSelected:NO animated:TRUE];
self.lastSelected = nil;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.accessoryView.backgroundColor = [UIColor grayColor]; // Not working
[cell setSelected:TRUE animated:TRUE];

UITableViewCell *old = [self.searchDisplayController.searchResultsTableView cellForRowAtIndexPath:self.lastSelected];
old.accessoryType = UITableViewCellAccessoryNone;
[old setSelected:NO animated:TRUE];
self.lastSelected = indexPath;
}

如何让蓝色也显示为 [UIColor grayColor]?

最佳答案

您正在更改内容 View 的背景颜色,它只是单元格 View 的一部分。

UITableViewCell representation

改变整个单元格的背景颜色。但是,您不能在 tableView:didDeselectRowAtIndexPath: 中执行此操作,因为它不会像 here 中解释的那样工作。 .

Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView) you must do it in the tableView:willDisplayCell:forRowAtIndexPath: method of the delegate and not in tableView:cellForRowAtIndexPath: of the data source.

在您的情况下,通过将索引保存在 ivar 中并重新加载 TableView 来跟踪在 tableView:didSelectRowAtIndexPath: 中选择的行。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
_savedIndex = indexPath;
[tableView reloadData];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([_savedIndex isEqual:indexPath]) {
cell.backgroundColor = [UIColor grayColor];
}
}

关于iOS:背景颜色——UITableView 和 Accessory 具有相同的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16112143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com