Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 786
Warrior Avatar asked Feb 15 '10 18:02

Warrior


People also ask

How do I turn off cell selection?

You need to set the selectionStyle property on the cell: cell. selectionStyle = UITableViewCellSelectionStyle. None in Swift, or cell.

Is it possible to add UITableView within a UITableViewCell?

yes it is possible, I added the UITableVIew within the UITableView cell .. :) no need to add tableview cell in xib file - just subclass the UITableviewCell and use the code below, a cell will be created programatically.

How can we use a reusable cell in UITableView?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.


1 Answers

You just have to put this code into cellForRowAtIndexPath

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

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

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

// Default style cell.selectionStyle = UITableViewCellSelectionStyleBlue;  // Gray style cell.selectionStyle = UITableViewCellSelectionStyleGray; 

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 gray out the item.

like image 109
Pugalmuni Avatar answered Sep 18 '22 00:09

Pugalmuni