如何取消突出显示表格单元格?
How to un-highlight tableview cell?
我正在使用 didSelectRowAtIndexPath
:
aCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? UITableViewCell
其中 aCell 是 class 级别变量。
这工作正常,但我注意到点击的单元格仍然是灰色的。单击另一个单元格时,有没有办法取消突出显示它们?
我可以存储之前单击的 NSIndexPath,但我不确定如何访问任何类型的单元格取消突出显示方法或 属性。
更改选定的单元格视图 属性 以使用清晰的颜色查看。如果您需要代码,请告诉我
更改选择样式 (Swift 2) 你可以在故事板上做同样的事情。
cell.selectionStyle = UITableViewCellSelectionStyle.None
创建自定义视图
让 selectedView = UIView()
selectedView.backgroundColor = UIColor.clearColor()
cell.selectedBackgroundView = selectedView
1。如何获取对可见单元格的引用
从你的问题中看不完全清楚,但看起来你在 tableView(_:didDeselectRowAtIndexPath:)
.
中使用了 dequeueReusableCell(withIdentifier:forIndexPath:)
你不应该那样做。
dequeueReusableCell(withIdentifier:forIndexPath:)
旨在用于配置单元格的 tableView(_:cellForRowAtIndexPath:)
内部。它要么创建并初始化一个 new 单元格,要么 return 一个 单元格以供重用 滚动到屏幕之外。重点是:它永远不会 return 指向当前可见的单元格的指针。
如果您想获取对当前可见单元格的引用,请在 table 视图中使用 cellForRowAtIndexPath(_:)
。
2。正确取消选择单元格
如果您只想取消选择某个索引路径中的单元格
您甚至不需要获取对实际单元格的引用。您可以通过将行的 indexPath
传递给 table 视图的 deselect 方法来简单地实现所需的行为。例如,如果您想在用户点击某个单元格后立即取消选择该单元格,您可以这样做:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
3。更多信息
更多信息请参考Table View Programming Guide for iOS and the documentation of the method dequeueReusableCell(withIdentifier:forIndexPath:)
in the UITableView Class Reference。
当您想要删除单元格的选定状态时,您需要调用 set the selected state to 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.
}
我正在使用 didSelectRowAtIndexPath
:
aCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? UITableViewCell
其中 aCell 是 class 级别变量。
这工作正常,但我注意到点击的单元格仍然是灰色的。单击另一个单元格时,有没有办法取消突出显示它们?
我可以存储之前单击的 NSIndexPath,但我不确定如何访问任何类型的单元格取消突出显示方法或 属性。
更改选定的单元格视图 属性 以使用清晰的颜色查看。如果您需要代码,请告诉我
更改选择样式 (Swift 2) 你可以在故事板上做同样的事情。
cell.selectionStyle = UITableViewCellSelectionStyle.None
创建自定义视图
让 selectedView = UIView()
selectedView.backgroundColor = UIColor.clearColor()
cell.selectedBackgroundView = selectedView
1。如何获取对可见单元格的引用
从你的问题中看不完全清楚,但看起来你在 tableView(_:didDeselectRowAtIndexPath:)
.
dequeueReusableCell(withIdentifier:forIndexPath:)
你不应该那样做。
dequeueReusableCell(withIdentifier:forIndexPath:)
旨在用于配置单元格的 tableView(_:cellForRowAtIndexPath:)
内部。它要么创建并初始化一个 new 单元格,要么 return 一个 单元格以供重用 滚动到屏幕之外。重点是:它永远不会 return 指向当前可见的单元格的指针。
如果您想获取对当前可见单元格的引用,请在 table 视图中使用 cellForRowAtIndexPath(_:)
。
2。正确取消选择单元格
如果您只想取消选择某个索引路径中的单元格
您甚至不需要获取对实际单元格的引用。您可以通过将行的 indexPath
传递给 table 视图的 deselect 方法来简单地实现所需的行为。例如,如果您想在用户点击某个单元格后立即取消选择该单元格,您可以这样做:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
3。更多信息
更多信息请参考Table View Programming Guide for iOS and the documentation of the method dequeueReusableCell(withIdentifier:forIndexPath:)
in the UITableView Class Reference。
当您想要删除单元格的选定状态时,您需要调用 set the selected state to 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.
}