Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make cell not clickable, but a button on it should still be clickable

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".

like image 376
user2650439 Avatar asked Aug 12 '13 14:08

user2650439


2 Answers

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.

like image 176
Lucas Eduardo Avatar answered Oct 18 '22 11:10

Lucas Eduardo


Swift version:

cell.selectionStyle = .none
like image 44
Jonas Deichelmann Avatar answered Oct 18 '22 11:10

Jonas Deichelmann