views:

1456

answers:

3

I am new to iPhone development. 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? Please help me out.

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

if(indexPath.section!=0)
if(indexPath.row==0)    

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

Thanks.

+3  A: 

You trap selections with these data source methods.

– tableView:willSelectRowAtIndexPath: 
– tableView:didSelectRowAtIndexPath: 
– tableView:willDeselectRowAtIndexPath: 
– tableView:didDeselectRowAtIndexPath:

In these methods, you check if the selected row is one you want to be selectable. If it is, take an action, if not, do nothing.

Unfortunately, you can't turn off selection for just one section. It's the whole table or nothing.

You can however set the table cells selectionStyle property to UITableViewCellSelectionStyleNone. I believe that will make the selection invisible. Combined with the above methods that should make the cells look completely inert from the user's perspective.

Edit01:

If you have a table in which only some of the rows are selectable it is important that the cells of the selectable rows be visually distinct from the non-selectable rows. The chevron accessory button is the default way to do this.

However you do it, you don't want your users trying to select rows and thinking the app has malfed because the row doesn't do anything.

TechZen
please elaborate how to use these methods or give some sample program links
Warrior
@Warrior: If you have installed the SDK you can open up Xcode, go to `Help -> Developer documentation`, then copy those method names into the search field to see how it works. The Developer documentation also contains sample code.
KennyTM
+5  A: 

hi,

You have to just put this code into "cellForRowAtIndexPath"

For disable the cell select:(While Click the cell)

cell.selectionStyle = UITableViewCellSelectionStyleNone;

For enable the cell:(While Click the cell)

 cell.selectionStyle = UITableViewCellSelectionStyleBlue;(By Default)

 cell.selectionStyle = UITableViewCellSelectionStyleGray;
Pugal Devan
hey dude you made it easy.Thanks.
Warrior
+1  A: 

If you want to make a row (or subset of rows) non-selectable, implement the UITableViewDelegate method -tableView:willSelectRowAtIndexPath: (also mentioned by TechZen). If the indexPath should be not be selectable, return nil, otherwise return the indexPath. To get the default selection behavior, you just return the indexPath passed to your delegate method, but you can also alter the row selection by returning a different indexPath.

example:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // rows in section 0 should not be selectable
    if ( indexPath.section == 0 ) return nil;

    // first 3 rows in any section should not be selectable
    if ( indexPath.row =< 2 ) return nil;

    // By default, allow row to be selected
    return indexPath;
}
Brian Chapados