lu yuan
lu yuan

Reputation: 7227

how could i forbid a tableviewcell to be selected

As usual, when a tableviewcell is touched, it will be selected. But now i need one cell in my tableview never be selected.how could i forbid a tableviewcell to be selected?

After modifying didSelectRowForIndexPath: method. It works now. Thx ALl:) Once the cell (need to be forbidden) is selected i just use the selectRowAtIndexPath method to select the former selected cell to be selected again.

Upvotes: 1

Views: 189

Answers (4)

Matthias Bauch
Matthias Bauch

Reputation: 90117

The correct way is to use tableView:willSelectRowAtIndexPath:.
Unlike all the other answers this will work with segues too ;-)

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath should not be selected) {
        // if another indexPath should be selected instead return this indexPath
        return nil;
    }
    return indexPath;
}

Upvotes: 2

Novarg
Novarg

Reputation: 7440

In your -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method add the following line to the cell you want to be not selectable:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Hope it helps

EDIT

And in your -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method add the following at the beginning:

if (indexPath == indexPathOfTheCellThatShouldntBeSelected) return;

Upvotes: 1

tipycalFlow
tipycalFlow

Reputation: 7644

In your didSelectRowForIndexPath: method, you can check if the cell meets your quality standards and accordingly, show a UIAlertView or go ahead if it meets the requirements

Upvotes: 1

Deviator
Deviator

Reputation: 688

You can disable the user Interaction for that very cell or set the selection Style None for that cell. Hope that helps!

Upvotes: 1

Related Questions