Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView';取消选择行索引路径阻止单元格';从更改背景颜色中删除子视图_Ios_Uitableview - Fatal编程技术网

Ios UITableView';取消选择行索引路径阻止单元格';从更改背景颜色中删除子视图

Ios UITableView';取消选择行索引路径阻止单元格';从更改背景颜色中删除子视图,ios,uitableview,Ios,Uitableview,我正在使用自定义单元格处理tableview。我需要通过设置标题标签的背景色来突出显示当前选定(活动)单元格,标题标签是cell.contentView的直接子视图。我的代码逻辑如下(为了更好地理解而修改): 问题是,当选择一个新单元格时,高亮显示的背景色会出现很短的时间,大约半秒,然后消失。如果我注释掉取消rowatindexpath的调用 // [tableView deselectRowAtIndexPath:indexPath animated:YES]; 高亮显示的背景色将保留。我的

我正在使用自定义单元格处理tableview。我需要通过设置标题标签的背景色来突出显示当前选定(活动)单元格,标题标签是cell.contentView的直接子视图。我的代码逻辑如下(为了更好地理解而修改):

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

// [tableView deselectRowAtIndexPath:indexPath animated:YES];
高亮显示的背景色将保留。我的猜测是,取消rowatindexpath会记住所有子视图以前的背景颜色,当它从着色背景恢复单元格时,会将所有子视图的背景颜色更改回原来的颜色

我的解决方法是添加如下延迟:

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秒,但没有成功


用幻数设置延迟时间是一种令人不快的方法。我的问题是,在tableview的didSelectRowAtIndexPath委托方法中,有没有更好的方法来设置单元格子视图的背景色?提前感谢。

您是否尝试创建从UITableViewCell继承的自定义单元格


自定义单元格将有一个标签作为公共属性,因此您可以从控制器引用标签,而无需标记。

您是否尝试创建从UITableViewCell继承的自定义单元格


自定义单元格将有一个标签作为公共属性,因此您可以从控制器引用标签,而无需标记。

创建一个临时变量,该变量将在视图控制器中保存选定单元格。我使用的是自定义单元格。不过这在这里并不重要

var selectedCellIndex:IndexPath?
在您的视图中,像这样初始化它

selectedCellIndex = IndexPath.init(row: -1, section: 0)// first row starts with 0 you know.
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

}
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.

}
在cellforrowatinexpath函数中,如下修改它

selectedCellIndex = IndexPath.init(row: -1, section: 0)// first row starts with 0 you know.
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

}
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.

}
像这样更改您的DidSelectRowatineXpath

selectedCellIndex = IndexPath.init(row: -1, section: 0)// first row starts with 0 you know.
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

}
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.

}
以下是输出:


创建一个临时变量,用于保存视图控制器中选定的单元格。我使用的是自定义单元格。不过这在这里并不重要

var selectedCellIndex:IndexPath?
在您的视图中,像这样初始化它

selectedCellIndex = IndexPath.init(row: -1, section: 0)// first row starts with 0 you know.
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

}
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.

}
在cellforrowatinexpath函数中,如下修改它

selectedCellIndex = IndexPath.init(row: -1, section: 0)// first row starts with 0 you know.
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

}
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.

}
像这样更改您的DidSelectRowatineXpath

selectedCellIndex = IndexPath.init(row: -1, section: 0)// first row starts with 0 you know.
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

}
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.

}
以下是输出:


为什么不覆盖CustomCollectionViewCell的
setSelected:
,并将正确的代码放在那里?不要忘记在
preparefore
中删除它,在
cellForRowAtIndexPath:
中,如果需要,将单元格标记为选中(因为滚动)。有些人可能建议通过tableview的重新加载数据方法更新高亮显示。在我的具体案例中,我避免这样做,因为更新是一个耗时的过程,有一个可感知的延迟。不,我没有。您有一个用于CustomCollectionViewCell的文件(h,m)。不在那个里覆盖
setSelected:
,颜色很好。哇!伟大的解决方案!非常感谢Larme@邝:希望你能理解。为什么不重写CustomCollectionViewCell的
setSelected:
,并在那里输入正确的代码呢?不要忘记在
preparefore
中删除它,在
cellForRowAtIndexPath:
中,如果需要,将单元格标记为选中(因为滚动)。有些人可能建议通过tableview的重新加载数据方法更新高亮显示。在我的具体案例中,我避免这样做,因为更新是一个耗时的过程,有一个可感知的延迟。不,我没有。您有一个用于CustomCollectionViewCell的文件(h,m)。不在那个里覆盖
setSelected:
,颜色很好。哇!伟大的解决方案!非常感谢Larme@匡:希望你能理解。哦,是的,另一个很棒的解决方案!谢谢!哦,是的,另一个很棒的解决方案!谢谢!是的,它是从UITableViewCell继承创建的,我将尝试访问公共属性。谢谢是的,它是从UITableViewCell继承创建的,我将尝试访问公共属性。谢谢