ios - Swift UITableView willSelectRowAt 无法正常工作

标签 ios swift uitableview

在我的 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/

相关文章:

iOS segue 两次屏幕

iphone - 无法继承NSNumber,NSNull ...,为什么?

objective-c - 使应用程序适用于 ios 3.0

ios - TableViewCell 上的 UIImageView 缺少对单击单元格的约束

ios - 使用 ARC 加载许多 .jpg 文件时收到内存警告和崩溃

ios - 停止/暂停在 tableview 单元格类中创建的 avplayer

ios - Swift - 无法使 PickerView 库正常工作

ios - 如何将 subview 添加到自定义 UICollectionViewCell

ios - 在ios应用程序上缓存图像的最佳方式?

ios - UITableView、搜索栏和部分索引