Warrior Warrior - 1 year ago 148
iOS Question

UITableview: How to Disable Selection for Some Rows but Not Others

I am displaying in a group

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
to my tube
player view

How can I make just specific groups or rows selectable?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:tubeUrl]];


Answer Source

You just have to put this code into cellForRowAtIndexPath

To disable the cell's selection property:(While tapping the cell).

cell.selectionStyle = UITableViewCellSelectionStyle.None

To enable being able to select (tap) thecell:(tapping the cell).

cell.selectionStyle = UITableViewCellSelectionStyle.Blue(By Default)

cell.selectionStyle = UITableViewCellSelectionStyle.Gray

Note that a cell with selectionStyle = UITableViewCellSelectionStyleNone will still cause the UI to call didSelectRowAtIndexPath when touched by the user. To avoid this, do as suggested below and set.

cell.userInteractionEnabled = NO

instead. Also note you may want to set cell.textLabel.enabled = NO to grey out the item.