views:

384

answers:

2

Is there any way to disable the "selectibility" of a UITableView row?

Example: I would like to make an appointment for a haircut at a certain time with a certain stylist. What gets displayed to a user is a list of times in half hour increments, but unavailable times are grayed out/unselectable.

+3  A: 

Yes, set the selectionStyle for any cells that shouldn't be selectable to UITableViewCellSelectionStyleNone.

Make sure to also set the cell's that should be selectable to UITableViewCellSelectionStyleBlue if you're using a reuseIdentifier on the cells.

You also need to override didSelectRowAtIndexPath so that cells with a UITableViewCellSelectionStyleNone style are ignored.

Ben S
I believe this will just change the colour of the cell when tapped from blue (default) to none - you'll still need to filter out the taps in your didSelectRowAtIndexPath method, and if you want the row to be grayed out, you'll need to handle that yourself in the cellForRowAtIndexPath method by changing the background and/or font colours (or possibly adding a semi-opaque view on top of the cell content.)
jasondoucette
How exactly would I filter the taps so that the didSelectRowAtIndexPath actions do not get fired when a disabled row is pressed?
rson
It depends on how you've got things set up, but inside didSelectRowAtIndexPath you'll know the table section ([indexPath section]) and row ([indexPath row]) and from there you'll be able to map it to your app data to figure out if the row's enabled (in which case you'll do stuff) or disabled (in which case you'll do nothing)
jasondoucette
Another filtering option is to implement tableView:willSelectRowAtIndexPath: - if you return nil from that the row being pressed will not be selected.
Kendall Helmstetter Gelner
@Kendall: Thanks for pointing that out. I didn't know of that method.
Ben S
A: 

Implement:

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

Do nothing inside of this method for rows that should be read only.

drewh
This will still highlight the tapped row. You need to set it's style like in my answer.
Ben S