如何取消突出显示tableview单元格?

时间:2016-07-16 18:08:26

标签: ios uitableview swift2 tableviewcell

我正在使用didSelectRowAtIndexPath

aCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? UITableViewCell

其中aCell是类级变量。

这很好但我发现点击的细胞仍然是灰色的。有没有办法在单击另一个单元格时取消突出显示它们?

我可以存储以前单击的NSIndexPath,但我不确定如何访问任何类型的单元格非突出显示方法或属性。

3 个答案:

答案 0 :(得分:2)

1。如何获取对可见单元格的引用

您的问题并不完全清楚,但看起来您在dequeueReusableCell(withIdentifier:forIndexPath:)内使用tableView(_:didDeselectRowAtIndexPath:)

你不应该这样做。

dequeueReusableCell(withIdentifier:forIndexPath:)旨在用于配置单元格的tableView(_:cellForRowAtIndexPath:)内。它要么创建并初始化 new 单元格,要么返回从屏幕滚动的单元格以供重复使用。重点是:它永远不会返回指向当前可见的单元格的指针。

如果您想获取对当前可见单元格的引用,请使用cellForRowAtIndexPath(_:)代替表格视图。

2。正确取消选择单元格

如果您只想取消选择某个索引路径的单元格 你甚至不需要获得对实际单元格的引用。您可以通过将行的indexPath传递给表视图的取消选择方法来简单地实现所需的行为。例如,如果您想在用户点击它之后立即取消选择一个单元格,您可以这样做:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

3。更多信息

有关详细信息,请参阅Table View Programming Guide for iOS中的UITableView Class Reference和方法dequeueReusableCell(withIdentifier:forIndexPath:)的文档。

答案 1 :(得分:1)

如果要删除单元格的选定状态,则需要调用将所选状态设置为false 在didSelectRowAtIndexPath:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("Is this function working??")
    let cell = tableView.cellForRowAtIndexPath(indexPath) //get the correct cell     

    cell?.selected = false //Will no longer be selected. Gray tableviewcell will now be normal.
}

答案 2 :(得分:0)

将单元格的选定视图属性更改为具有清晰颜色的视图。如果您需要代码,请告诉我

  1. 更改选择样式(Swift 2)您可以在故事板上执行相同操作。

    cell.selectionStyle = UITableViewCellSelectionStyle.None

  2. 创建自定义视图

    让selectedView = UIView()

    selectedView.backgroundColor = UIColor.clearColor()

    cell.selectedBackgroundView = selectedView

相关问题