如何使UITableView上的单元格无法选择?

She*_*lam 64 iphone cocoa-touch objective-c uitableview

我有一个单元格,我插入到UITableView的顶部.如何确保当用户点击单元格时,它不会显示蓝色选定的指示符?

Joh*_*ang 70

UITableViewCell选择样式设置为UITableViewCellSelectionStyleNone

  • 这不会使细胞不可选择,只是意味着细胞的视觉外观在选择时不会改变.`tableView:didSelectRowAtIndexPath:`在点击单元格时仍会被调用.要实际阻止选择,您需要在`UITableViewDelegate`上实现tableView:willSelectRowAtIndexPath:](http://stackoverflow.com/a/812921/172218),并为不可选择的行返回nil. (75认同)
  • 或者简单地说:`cell.selectionStyle = .none` (3认同)

mal*_*hal 61

为了使单元格完全不可选,每个单元格需要两件事:

1-正如其他人所说:

cell.selectionStyle = UITableViewCellSelectionStyleNone;
Run Code Online (Sandbox Code Playgroud)

2-实现此委托方法如下:

// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    if(cell.selectionStyle == UITableViewCellSelectionStyleNone){
        return nil;
    }
    return indexPath;
}
Run Code Online (Sandbox Code Playgroud)


Edu*_*ias 16

从故事板中为表视图设置以下内容:

选择:没有选择

触摸显示选择:错误

在此输入图像描述


Anj*_*nju 13

你可以做

cell.selectionStyle = UITableViewCellSelectionStyleNone;
Run Code Online (Sandbox Code Playgroud)


小智 6

对于 Swift 3,您可以使用

 cell.isUserInteractionEnabled = false
Run Code Online (Sandbox Code Playgroud)


Dus*_*ams 5

Swift语法:

cell.selectionStyle = UITableViewCellSelectionStyle.None
Run Code Online (Sandbox Code Playgroud)


myt*_*der 5

我是从禁用TableViewCell的角度回答的.您可以使用故事板.

XCode Version 8.2.1 (8C1002)

在故事板上选择TableVewCell,右侧面板上将显示以下内容 - Utilities.

实用工具补充工具栏

进行选择:无

就这样!


小智 5

问题是如何使某些细胞可选择而其他细胞则不可选择。这是我的解决方案(在尝试了很多其他建议之后):

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    if (indexPath.row == 1 || indexPath.row == 2) {
        return indexPath
    }
    return nil        
}
Run Code Online (Sandbox Code Playgroud)