Disable selection of a single UITableViewCell

2020-05-15 13:31发布

问题:

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

回答1:

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

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

}


回答2:

To stop just some cells being selected use:

cell.userInteractionEnabled = NO;

As well as preventing selection, this also stops tableView:didSelectRowAtIndexPath: being called for the cells that have it set. It will also make voiceover treat it the same as a dimmed button (which may or may not be what you want).

Note that if you have interactive elements in the cell (ie. switches/buttons), you'll need to use cell.selectionStyle = UITableViewCellSelectionStyleNone; instead and then make sure to ignore taps on the cell in tableView:didSelectRowAtIndexPath:.



回答3:

Throw this in your custom Table 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;
}

Then to enable/disable selection for someTableViewCell, do this:

[self setEnabled:state forTableViewCell:someTableViewCell];

You're done and can ship.



回答4:

-(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



回答5:

As I mentioned in another thread all the above methods are not solving the problem precisely. The correct way of disabling a cell is through the method

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

and in that method one has to use

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

which disables cell selection but still allows the user to interact with subviews of the cell such as a UISwitch.



回答6:

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.



回答7:

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;
}


回答8:

Try this in swift:

self.tableView.deselectRowAtIndexPath(indexPath, animated: true)


回答9:

If anyone wondering how to achieve this in swift then here is my code. I am using Xcode 7 and tested using iPad Retina(iOS 9).

cell.selectionStyle = UITableViewCellSelectionStyle .None
cell.userInteractionEnabled = false

Try to place this two line code whether you want. In my case I have used this in this method for displaying cells.

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)

Remember this two line code will block any kind of selection or interaction to your cells but you can only use the first line individually if you want. That is...

cell.selectionStyle = UITableViewCellSelectionStyle .None

Only this line will block the selection to your cells.

However the second line will make the cell "Read-Only". That is..

cell.userInteractionEnabled = false

Thanks

Hope this helped.