I am displaying in a group tableview
contents parsed from XML. I want to disable the click event on it (I should not be able to click it at all) The table contains two groups. I want to disable selection for the first group only but not the second group. Clicking the first row of second group navigates
to my tube player view
.
How can I make just specific groups or rows selectable?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section!=0)
if(indexPath.row==0)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:tubeUrl]];
}
Thanks.
You just have to put this code into
cellForRowAtIndexPath
To disable the cell's selection property: (while tapping the cell)
To enable being able to select (tap) the cell: (tapping the cell)
Note that a cell with
selectionStyle = UITableViewCellSelectionStyleNone;
will still cause the UI to calldidSelectRowAtIndexPath
when touched by the user. To avoid this, do as suggested below and set.instead. Also note you may want to set
cell.textLabel.enabled = NO;
to gray out the item.None from the answers above really addresses the issue correctly. The reason is that we want to disable selection of the cell but not necessarily of subviews inside the cell.
In my case I was presenting a UISwitch in the middle of the row and I wanted to disable selection for the rest of the row (which is empty) but not for the switch! The proper way of doing that is hence in the method
where a statement of the form
disables selection for the specific cell while at the same time allows the user to manipulate the switch and hence use the appropriate selector. This is not true if somebody disables user interaction through the
method which merely prepares the cell and does not allow interaction with the UISwitch.
Moreover, using the method
in order to deselect the cell with a statement of the form
still shows the row being selected while the user presses on the original contentView of the cell.
Just my two cents. I am pretty sure many will find this useful.
If you want to make a row (or subset of rows) non-selectable, implement the
UITableViewDelegate
method-tableView:willSelectRowAtIndexPath:
(also mentioned by TechZen). If theindexPath
should be not be selectable, returnnil
, otherwise return theindexPath
. To get the default selection behavior, you just return theindexPath
passed to yourdelegate
method, but you can also alter the row selection by returning a differentindexPath
.example:
Use this to make the cell look like it is disabled and non-selectable:
Important: note that this is only a styling property, and does not actually disable the cell. In order to do that, you have to check for
selectionStyle
in yourdidSelectRowAtIndexPath:
delegate implementation:Implement just the method
tableView:willSelectRowAtIndexPath:
in the data source for your table. If you want the row at the path to highlight, return the given indexPath. If you do not, return nil.Example from my app:
The nice thing about this is that
shouldPerformSegueWithIdentifier:sender:
will not be called if the above method returns nil, although I repeat the test above just for completeness.I like Brian Chapados answer above. However, this means that you may have logic duplicated in both cellForRowAtIndexPath and then in willSelectRowAtIndexPath which can easily get out of sync. Instead of duplicating the logic, just check the selectionStyle: