Disable selection of a single UITableViewCell

前端 未结 9 2080
迷失自我
迷失自我 2021-01-31 08:32

How do you disable selecting only a single cell in a UITableView? I have several, and I only want the last to be disabled.

相关标签:
9条回答
  • 2021-01-31 09:18

    The cleanest solution that I have found to this only makes use of the delegate method willDisplayCell.

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if([indexPath row] == 0) //<-----ignores touches on first cell in the UITableView
        {                        //simply change this around to suit your needs
            cell.userInteractionEnabled = NO;
            cell.textLabel.enabled = NO;
            cell.detailTextLabel.enabled = NO;
        }
    }
    

    You don't have to take any further action in the delegate method didSelectRowAtIndexPath to ensure that the selection of this cell is ignored. All touches on this cell will be ignored and the text in the cell will be grayed out as well.

    0 讨论(0)
  • 2021-01-31 09:20
    -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([self numberOfRowsInSection] == [indexPath row]) {
            return nil;
        } else {
            return indexPath;
        }
    }
    

    the last row of the table will not be selected

    0 讨论(0)
  • 2021-01-31 09:23

    with iOS 6.

    You can use the following delegate method and return NO in case you don't it to be selected and YES in case you want it to be selected.

    - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return indexPath.section == 0;
    }
    
    0 讨论(0)
提交回复
热议问题