当你点击UITableView中的一行时,该行会高亮显示并被选中。是否有可能禁用这个,所以点击一行什么都不做?


当前回答

斯威夫特3,4和5

更好的做法是,在UITableViewCell中编写代码

例如,你有名为MyCell的UITableViewCell, 在awakeFromNib中只写self。selectionStyle = .none

完整的例子:

class MyCell: UITableViewCell {
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.selectionStyle = .none
    }
    
}

其他回答

试试这个

cell.selectionStyle = UITableViewCellSelectionStyleNone;

and

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

你也可以使用interfacebuilder设置选择样式。

试着输入:

cell.selected = NO;

它将在需要时取消选择您的行。

在Swift3中…

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let r = indexPath.row
    print("clicked .. \(r)")
    tableView.cellForRow(at: indexPath)?.setSelected(false, animated: true)
}

你可以在(iOS) Xcode 9, Swift 4.0中使用以下代码禁用表格单元格高亮显示

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


        let cell = tableView.dequeueReusableCell(withIdentifier: "OpenTbCell") as! OpenTbCell
        cell.selectionStyle = .none
        return cell


}

你也可以将背景颜色设置为Clear,以达到与UITableViewCellSelectionStyleNone相同的效果,以防你不想/不能使用UITableViewCellSelectionStyleNone。

您将使用如下代码:

UIView *backgroundColorView = [[UIView alloc] init];
backgroundColorView.backgroundColor = [UIColor clearColor];
backgroundColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView: backgroundColorView];

这可能会降低您的性能,因为您添加了一个额外的彩色视图到每个单元格。

这就是我所使用的,在cellForRowAtIndexPath中编写这段代码。

cell.selectionStyle = UITableViewCellSelectionStyleNone;