在我的 tableView 中,我希望单元格不可选择。
所以我在 TableViewController 中写道:
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if indexPath.section == 0 {
return nil
} else {
if switchIsOn! {
return indexPath
} else {
return nil
}
}
}
当我在单元格中进行正常触摸时,它会按预期工作,但是当我将手指按住单元格时,它无论如何都会被选中。我怎样才能避免这种情况?
最佳答案
当开关状态改变时调用tableView.reloadData(),然后添加这个
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
if indexPath.section != 0 && !switchIsOn! {
cell.selectionStyle = .none
} else {
cell.selectionStyle = .default
}
return cell
}
关于ios - Swift UITableView willSelectRowAt 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46259252/