gpt4 book ai didi

ios - UITableView 的 deselectRowAtIndexPath 防止 cell 的 subview 改变背景颜色

转载 作者:行者123 更新时间:2023-11-28 19:31:14 24 4
gpt4 key购买 nike

我正在处理带有自定义单元格的表格 View 。我需要通过为标题标签设置背景颜色来突出显示当前选定的(事件的)单元格,这是 cell.contentView 的直接 subview 。我的代码逻辑是这样的(为了更好理解而修改):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];

UITableViewCell *previousActiveCell = [tableView cellForRowAtIndexPath:_indexPathActiveCell]; // previous selection
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath: indexPath]; // current selection

UILabel *labelpreviousActiveCellTitle = [previousActiveCell.contentView viewWithTag:SUBVIEW_TAG_TITLE_LABEL];
UILabel *labelSelectedCellTitle = [selectedCell.contentView viewWithTag:SUBVIEW_TAG_TITLE_LABEL];

labelpreviousActiveCellTitle.backgroundColor = [UIColor clearColor]; // remove highlighting from previous selection
labelSelectedCellTitle.backgroundColor = [UIColor redColor]; // highlighted

_indexPathActiveCell = indexPath; // update _indexPathActiveCell with current selection
}

问题是,当选择一个新的单元格时,突出显示的背景颜色会出现很短的时间,大约半秒,然后消失。如果我注释掉对 deselectRowAtIndexPath 的调用,

// [tableView deselectRowAtIndexPath:indexPath animated:YES];

突出显示的背景颜色将保留。我的猜测是 deselectRowAtIndexPath 记住所有 subview 以前的背景颜色,当它从阴影背景中恢复单元格时,它会将所有 subview 的背景颜色改回来。

我的解决方法是像这样添加延迟:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
labelpreviousActiveCellTitle.backgroundColor = [UIColor clearColor];
labelSelectedCellTitle.backgroundColor = [UIColor redColor];
});

并且有效。请注意,我还尝试了更短的延迟,例如 0.01 秒,但没有用。

用魔数(Magic Number)设置延迟时间是一种令人不快的方式。我的问题是,有没有更好的方法在 tableview 的 didSelectRowAtIndexPath 委托(delegate)方法中为单元格的 subview 设置背景颜色?提前致谢。

最佳答案

创建一个临时变量,它将在您的 View Controller 中保存选定的单元格。我正在使用自定义单元格。不过这里没那么重要

var selectedCellIndex:IndexPath?

在你的 viewDidLoad 中像这样初始化它。

selectedCellIndex = IndexPath.init(row: -1, section: 0)// first row starts with 0 you know.

在你的 cellForRowAtIndexPath 函数中像这样修改它。

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "chatCell", for: indexPath) as! sampleTableViewCell


if indexPath.row == selectedCellIndex?.row {
cell.label.backgroundColor = UIColor.green
}
else {
cell.label.backgroundColor = UIColor.clear
}
cell.selectionStyle = .none //For removing grey color while cell selection.
return cell

}

像这样更改您的 didSelectRowAtIndexPath。

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedCellIndex = indexPath
tableView.reloadRows(at: tableView.indexPathsForVisibleRows!, with: .none) //reload only visible cells. don't call reloadData it's expensive.

}

这是输出:

enter image description here

关于ios - UITableView 的 deselectRowAtIndexPath 防止 cell 的 subview 改变背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44441097/

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