禁用单个UITableViewCell的选择

Dex*_*erW 35 iphone cocoa-touch uitableview ios

如何禁用在UITableView中仅选择单个单元格?我有几个,我只希望最后一个被禁用.

Cam*_*hez 52

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = ...

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

}
Run Code Online (Sandbox Code Playgroud)

  • 这将阻止选择单元格,但仍会调用`tableView:didSelectRowAtIndexPath:`. (34认同)
  • 请参阅我的答案,了解真正有效的清洁解决方案. (2认同)

Jos*_*phH 52

要停止选择某些单元格,请使用:

cell.userInteractionEnabled = NO;
Run Code Online (Sandbox Code Playgroud)

除了阻止选择之外,这还会阻止tableView:didSelectRowAtIndexPath:为设置它的单元格调用.它也会使画外音像暗灰色按钮一样(可能是你想要的也可能不是).

请注意,如果单元格中有交互元素(即开关/按钮),则需要使用cell.selectionStyle = UITableViewCellSelectionStyleNone;,然后确保忽略单元格中的点击tableView:didSelectRowAtIndexPath:.

  • 我不能使用它,因为它还禁用了`cell.accessoryView`,我将其设置为我希望可以替换的`UIButton`. (4认同)

小智 19

把它放在你的自定义表VC中:

// cells lacking UITableViewCellAccessoryDisclosureIndicator will not be selectable
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType != UITableViewCellAccessoryDisclosureIndicator) {
        return nil;
    }
    return indexPath;
}

// disabled cells will still have userinteraction enabled for their subviews
- (void)setEnabled:(BOOL)enabled forTableViewCell:(UITableViewCell *)tableViewCell
{
    tableViewCell.accessoryType = (enabled) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
    // if you dont want the blue selection on tap, comment out the following line
    tableViewCell.selectionStyle = (enabled) ? UITableViewCellSelectionStyleBlue : UITableViewCellSelectionStyleNone;
}
Run Code Online (Sandbox Code Playgroud)

然后启用/禁用someTableViewCell的选择,执行以下操作:

[self setEnabled:state forTableViewCell:someTableViewCell];
Run Code Online (Sandbox Code Playgroud)

你完成了,可以发货.


Aar*_*ers 10

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self numberOfRowsInSection] == [indexPath row]) {
        return nil;
    } else {
        return indexPath;
    }
}
Run Code Online (Sandbox Code Playgroud)

不会选择表格的最后一行

  • 对不起,需要在单元格中添加UITableViewCellSelectionStyleNone样式 (2认同)