Question

I've a tableView with some cells. Each cell also contains a button. When the user clicks the button, the cell should be unclickable, but not the button. So when the user clicks on the button of a cell which is not clickable, this cell should be clickable again.

I tried:

cell.userInteractionEnabled = NO;

...but then the button wasn't clickable anymore.

Thanks to your effort in advance.

EDIT I mean: When I click on a cell a new view opens. But I want, that no action happens, when the cell is not "clickable".

Was it helpful?

Solution

Unclickable in which way? If you just want the cell to not be selectable, you are probably seeking for this:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

If you want to prevent your code to be executed when the selection is disabled, just check for the selection property inside your didSelectRowAtIndexPath:method. Something like this:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.selectionStyle != UITableViewCellSelectionStyleNone) {
        //(your code opening a new view)
    }
}

Remember, you still have to play with this property, setting to UITableViewCellSelectionStyleNone when you don't want the cell to be selectable, and setting back to UITableViewCellSelectionStyleBlue (or UITableViewCellSelectionStyleGray) when you want it to be selectable again.

OTHER TIPS

Swift version:

cell.selectionStyle = .none

Remove selection by setting UITableViewCellSelectionStyleNone as the selectionStyle.

cell.selectionStyle = UITableViewCellSelectionStyleNone;

And do nothing in -tableView:didSelectRowAtIndexPath:

You can be selective in that delegate method for example if only the first row in the first section has the button and should do nothing :

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *indexPathForDisabledCell = [NSIndexPath indexPathForRow:0
                                                               inSection:0];
    if([indexPath compare:indexPathForDisabledCell] != NSOrderedSame) {
        //Do whatever you do with other cells
    }
}

This can also be done through Interface Builder using User Defined Runtime Attributes on the TableViewCell:

Key Path | Type | Value

selectionStyle | Number | 0

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/c/tdef/UITableViewCellStyle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top