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

I have a tableView with some cells. Each cell also contains a button. When the user clicks the button, the cell should be invisible, but not the button. Therefore, when a user clicks on a button on a cell that is not clickable, that cell must be pressed again.

I tried:

cell.userInteractionEnabled = NO; 

... but then the button was no longer pressed.

Thanks to your efforts in advance.

EDIT I mean: when I click on a cell, a new view opens. But I want no action to be taken when the cell is not clickable.

+9
source share
4 answers

The untouchable, how? If you want the cell not to be selected, you are probably looking for this:

 cell.selectionStyle = UITableViewCellSelectionStyleNone; 

If you want your code to not execute when the selection is turned off, just check the selection property inside your didSelectRowAtIndexPath: method. Something like that:

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

Remember that you still have to play with this property by setting to UITableViewCellSelectionStyleNone when you do not want the cell to be selected, and returning to UITableViewCellSelectionStyleBlue (or UITableViewCellSelectionStyleGray ) when you want it to be selectable again.

+15
source

Remove the selection by setting UITableViewCellSelectionStyleNone as selectionStyle .

 cell.selectionStyle = UITableViewCellSelectionStyleNone; 

And do nothing in -tableView:didSelectRowAtIndexPath:

You can be selective in this deletion, for example, if only the first line in the first section has a button and should not do anything:

 -(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 } } 
0
source

You can also do this using Interface Builder using the Runtime Customization Attributes in TableViewCell:

Key Path | Type | Value

selectionStyle | Number | 0

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

0
source

Swift version:

 cell.selectionStyle = .none 
0
source

Source: https://habr.com/ru/post/951562/


All Articles