UITableView Setting some cells as "unselectable"

IphoneCocoa Touch

Iphone Problem Overview


How can I set the UITableView's cell property to be unselectable? I don't want to see that blue selection box when the user taps on the cell.

Iphone Solutions


Solution 1 - Iphone

To Prevent Row Selection

To completely prevent selection of the UITableViewCell, have your UITableViewDelegate implement tableView:willSelectRowAtIndexPath:. From that method you can return nil if you do not want the row to be selected.

- (NSIndexPath *)tableView:(UITableView *)tv willSelectRowAtIndexPath:(NSIndexPath *)path
{
    // Determine if row is selectable based on the NSIndexPath.

    if (rowIsSelectable) {
        return path;
    }
    return nil;
}

This prevents the row from being selected and tableView:didSelectRowAtIndexPath: from being called. Note, however, that this does not prevent the row from being highlighted.

To Prevent Row Highlighting

If you would like to prevent the row from being visually highlighted on touch, you can ensure that the cell's selectionStyle is set to UITableViewCellSelectionStyleNone, or preferably you can have your UITableViewDelegate implement tableView:shouldHighlightRowAtIndexPath: as follows:

- (BOOL)tableView:(UITableView *)tv shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Determine if row is selectable based on the NSIndexPath.

    return rowIsSelectable;
}

Solution 2 - Iphone

Set the table cell's selectionStyle property to UITableViewCellSelectionStyleNone. That should prevent it from highlighting, and you can also check that property in your tableView:didSelectRowAtIndexPath:.

Solution 3 - Iphone

use this:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Solution 4 - Iphone

For iOS 6+ only.

You can implement the method tableView:shouldHighlightRowAtIndexPath: in your delegate. Read more here : http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html

Solution 5 - Iphone

Had this problem, too, tried everything already mentioned. The final trick, which got rid of the "blue flash" at selecting a table cell was adding this line:

self.myTableView.allowsSelection = NO;

Not sure whether it was this one line or everything combined, but as total grand result I get no more blue selection or even the blue flash. Happy!

Solution 6 - Iphone

Set cell.userInteractionEnabled = NO;

Solution 7 - Iphone

using IB is also an elegant way:

enter image description here

Solution 8 - Iphone

Apple says that the first thing you should do in didSelectRowAtIndexPath is to deselect the row

[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];

Then you can change the AccessoryType to be a checkmark, or none, etc. So when you enter didSelectRowAtIndexPath you could deselect the row, and if its not meant to be selected, simply don't check that row.

Table View Programming Guide

Solution 9 - Iphone

Another way is to add a couple category methods to UITableViewCell. I like this better than Sebastians (also good) answer because the way I'm building my table. I thought it might be helpful to someone else.

- (void)setSelectable:(BOOL)enabled {
    [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    [self setUserInteractionEnabled:enabled];
}

- (BOOL)isSelectable {
    BOOL disabled = [self selectionStyle]==UITableViewCellSelectionStyleNone &&
                     [self isUserInteractionEnabled];
    return ! disabled;
}

Solution 10 - Iphone

Swift 4:
You can prevent selection and highlighting by using the UITableViewDelegate: shouldHighlightRowAt

This answer assumes you have a custom cell created named: CustomTableViewCell
And that you created a boolean inside that custom cell named: isSelectable

func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
    let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
    if cell.isSelectable == false {
        return false
    } else {
        return true
    }
}

This is the Swift version of Sebastian Celis's objc answer.

Solution 11 - Iphone

If you have designed your cell in Interface Builder, you can do this by removing the checkbox from 'User Interaction Enabled' for the tableViewCell.

Solution 12 - Iphone

Swift 5

cell.isUserInteractionEnabled = false

Solution 13 - Iphone

There is another simple way to avoid the selection appearing as blue.

Select a cell you don't want to appear as blue.

Then select the attributes inspector (the shield icon next to the ruler icon on the properties view on the side).

Then change the 'Selection' field from 'Blue' to 'None'.

Note, presumably this is still selecting, it will just not appear as selected if all you want is to avoid the UI effect.

Solution 14 - Iphone

To make certain row unselected you have to make some changes in two methods of UITableView delegate.

In the method below

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

after allocation of the cell, write the code below

if(indexPath.row == someCellNumber) cell.selectionStyle =UITableViewCellSelectionStyleNone;

The above code will prevent highlighting the cell, if somehow user tries to selects.

in this delegate method below

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row == someCellNumber) return;
    
    //for other cells write the code below...
}

if you don't write if(indexPath.row == someCellNumber) return; row will still be selected and there is a chance of app crash

Solution 15 - Iphone

Use tableView: willDisplayCell: forRowAtIndexPath: instead of tableView: didSelectRowAtIndexPath: to get rid of the flash that appears first time you touch the cell.

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

Solution 16 - Iphone

Swift 4:

In the cell class:

selectionStyle = .none

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMugunthView Question on Stackoverflow
Solution 1 - IphoneSebastian CelisView Answer on Stackoverflow
Solution 2 - IphoneDaniel DickisonView Answer on Stackoverflow
Solution 3 - IphonehanumanDevView Answer on Stackoverflow
Solution 4 - IphoneAyush GoelView Answer on Stackoverflow
Solution 5 - IphoneJOMView Answer on Stackoverflow
Solution 6 - IphoneRob BonnerView Answer on Stackoverflow
Solution 7 - IphoneBrianView Answer on Stackoverflow
Solution 8 - IphoneryandayView Answer on Stackoverflow
Solution 9 - IphonezekelView Answer on Stackoverflow
Solution 10 - IphoneChase McElroyView Answer on Stackoverflow
Solution 11 - IphoneJayView Answer on Stackoverflow
Solution 12 - IphonesimsekView Answer on Stackoverflow
Solution 13 - IphonesienView Answer on Stackoverflow
Solution 14 - IphoneDipu RajakView Answer on Stackoverflow
Solution 15 - Iphoneuser3164248View Answer on Stackoverflow
Solution 16 - IphoneEdenView Answer on Stackoverflow