简体   繁体   中英

Swift UITableView willSelectRowAt doesn't work properly

In my tableView I want the cells to be unselectable.
So I wrote in my 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

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

    ...

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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