iOS / Swift:使表格视图单元格不可选择,但表格视图在Swift中仍可滚动

时间:2018-09-16 16:40:27

标签: ios swift tableview

我想使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)
    }

有人可以建议如何在不阻止滚动的情况下阻止选择吗?

预先感谢您的任何建议。

2 个答案:

答案 0 :(得分:1)

希望您正在寻找这个:

tableView.allowsSelection = false

答案 1 :(得分:1)

问题在于您没有返回出队相同单元格。取而代之的是,您使另一个单元出列并返回它没有设置的任何属性。

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

    if let cell = tableView.dequeueReusableCell(withIdentifier: "myMessageCell", for: indexPath) as? myMessageCell {
        cell.message = messages[indexPath.row]
        cell.selectionStyle = .none
        return cell // Return the cell here instead
    }

    // return tableView.dequeueReusableCell(withIdentifier: "myMessageCell", for: indexPath) // Return another cell which has none of your properties set.
    return UITableViewCell() // return default cell in case your cell is not dequeued
}