【发布时间】:2019-02-20 16:48:08
【问题描述】:
我想让 tableView 的单元格不可选,但仍允许滚动。当我放置
tableView.isUserInteractionEnabled = false
在viewDidLoad 的一些答案中推荐,它可以防止选择但也可以防止滚动。
添加:
cell.selectionStyle = .none
下面的cellforrowatindexpath 对我没有任何影响。
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
self.tableView.beginUpdates()
self.tableView.endUpdates()
if let cell = tableView.dequeueReusableCell(withIdentifier: "myMessageCell", for: indexPath) as? myMessageCell {
cell.message = messages[indexPath.row]
cell.selectionStyle = .none
}
return tableView.dequeueReusableCell(withIdentifier: "myMessageCell", for: indexPath)
}
谁能建议如何在不阻止滚动的情况下阻止选择?
提前感谢您的任何建议。
【问题讨论】:
-
现在选择单元格时会发生什么?颜色变了?应该解决的是您将
selectionStyle设置为.none -
“对我没有任何影响”?你期待什么效果?发生了什么?
-
单元格是否突出显示。将在上面添加更多代码。我期待它不会改变颜色,例如突出显示。
-
为什么是
self.tableView.beginUpdates(), self.tableView.endUpdates()? -
另外,如果您知道reuseIdentifier
"myMessageCell"将始终返回myMessageCell,那么这是使用as! myMessageCell的合适位置。您的故事板中可能存在强制展开会发现的问题。