UITableView Setting some cells as “unselectable”

后端 未结 16 1410

How can I set the UITableView\'s cell property to be unselectable? I don\'t want to see that blue selection box when the user taps on the cell.

相关标签:
16条回答
  • 2020-12-12 18:30

    Set cell.userInteractionEnabled = NO;

    0 讨论(0)
  • 2020-12-12 18:34

    If you have designed your cell in Interface Builder, you can do this by removing the checkbox from 'User Interaction Enabled' for the tableViewCell.

    0 讨论(0)
  • 2020-12-12 18:35

    Swift 4:
    You can prevent selection and highlighting by using the UITableViewDelegate: shouldHighlightRowAt

    This answer assumes you have a custom cell created named: CustomTableViewCell
    And that you created a boolean inside that custom cell named: isSelectable

    func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
        let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
        if cell.isSelectable == false {
            return false
        } else {
            return true
        }
    }
    

    This is the Swift version of Sebastian Celis's objc answer.

    0 讨论(0)
  • 2020-12-12 18:37

    Had this problem, too, tried everything already mentioned. The final trick, which got rid of the "blue flash" at selecting a table cell was adding this line:

    self.myTableView.allowsSelection = NO;
    

    Not sure whether it was this one line or everything combined, but as total grand result I get no more blue selection or even the blue flash. Happy!

    0 讨论(0)
提交回复
热议问题