Atma
Atma

Reputation: 29805

How to set a uitableviewcell unclickable on the iphone sdk

Hi I'm trying to have my uitableviewcell rendered inactive so that a user can't click but merely say the data in the cell. I attempt to do so with:

UITableViewCell *cell = nil;
if (indexPath.row < factsCount) {
static NSString *FactsCellIdentifier = @"FactsCell";
cell = [tableView dequeueReusableCellWithIdentifier:FactsCellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

However the cell can still be clicked and highlighted.

Thanks for your help!

Upvotes: 12

Views: 11758

Answers (4)

JosephH
JosephH

Reputation: 37505

To stop just some cells being selected use:

cell.userInteractionEnabled = NO;

As well as preventing selection, this also stops tableView:didSelectRowAtIndexPath: being called for the cells that have it set.

(but note that it will disable any interactive content within the cells - eg. buttons - for that case, set selectionStyle to UITableViewCellSelectionStyleNone and ignore the tableView:didSelectRowAtIndexPath: call for that cell)

Upvotes: 24

Vladimir
Vladimir

Reputation: 170849

To disable cell selection you can implement -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath method in table view delegate and return nil if you don't want cell with given NSIndexPath to be selected.
As already pointed in other answers to disable cell highlighting you should set its selectionStyle property to UITableViewCellSelectionStyleNone.
Also make sure that you set properties correctly when reusing tableviewcells

Upvotes: 4

Atma
Atma

Reputation: 29805

This can be done with:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Upvotes: 4

Ole Begemann
Ole Begemann

Reputation: 135578

Set the cell's selectionStyle to UITableViewCellSelectionStyleNone.

Upvotes: 11

Related Questions