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

4

12

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

Veil answered 12/8, 2013 at 14:38 Comment(2)
Try setting userInteractionEnabled on the individual controls.Luteolin
@MarcusAdams But how can I do this?Veil
T
19

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.

Tiphani answered 12/8, 2013 at 14:44 Comment(6)
But I can still click on the cell and the new viewController opens...the only difference I recognize is that their is no color when the cell is selected...or am I getting something wrong hereVeil
Edited. Try and see if it helped.Tiphani
@LucasEduardo Ok...I understand. Is it also possible to combine this with prepareForSegue method as I use storyboard?Veil
It depends in what you are trying to do. The problem is because in prepareForSegue you don't have a reference to the indexPath selected (and consequently the cell), so you would have to store the selected cell in some instance variable or something like that. If the anwser helped you, don't forget to accept it in order to remove your question from the Unanswered section!Tiphani
for swift 2 the syntax slightly differs: cell.selectionStyle = .None;Rubble
This is worked in Swift5. I have 4 buttons in my static tableView cell.Transvalue
I
3

Swift version:

cell.selectionStyle = .none
Intrinsic answered 13/12, 2018 at 13:52 Comment(0)
L
0

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
    }
}
Liegeman answered 12/8, 2013 at 15:12 Comment(0)
T
0

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

Turki answered 1/2, 2016 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.