简体   繁体   English

Swift UITableView willSelectRowAt无法正常工作

[英]Swift UITableView willSelectRowAt doesn't work properly

In my tableView I want the cells to be unselectable. 在我的tableView中,我希望单元格是不可选择的。
So I wrote in my TableViewController: 所以我在TableViewController中写道:

override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    if indexPath.section == 0 {
        return nil
    } else {
        if switchIsOn! {
            return indexPath
        } else {
            return nil
        }
    }

}


It works as expected when I make a normal touch in the cells, but when I hold my finger down on the cell, it becomes selected anyway. 当我在单元格中进行正常触摸时,它会按预期工作,但是当我将手指按住单元格时,无论如何它都会变为选中状态。 How can I avoid this? 如何避免这种情况?

When switch state changes call tableView.reloadData(), then add this 当开关状态更改时,调用tableView.reloadData(),然后添加

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    ...

    if indexPath.section != 0 && !switchIsOn! {
        cell.selectionStyle = .none
    } else {
        cell.selectionStyle = .default
    }
    return cell

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM