UITableView Setting some cells as "unselectable"
Asked Answered
F

16

112

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.

Floro answered 1/5, 2009 at 17:45 Comment(1)
Note recent googlers: The way to do this as of iOS 6 and greater is tableView:shouldHighlightRowAtIndexPath: as noted by Ayush belowRambo
E
154

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:.

Erythrite answered 1/5, 2009 at 19:54 Comment(3)
Kendall is right. Follow Sebastian Celis' answer to prevent didSelectRowAtIndexPath from being called in the first place. You should also set selectionStyle, though, to prevent the highlighting.Erythrite
yeah i suppose the Sebastian Celis' answer to be the correct answer, thanks DanielBastille
Storyboard fans using static TableViews can select a UITableViewCell and under Attributes Inspector can find the Selection Field and set it to NoneTemperament
S
204

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;
}
Someway answered 1/5, 2009 at 19:43 Comment(5)
This should be the accepted answer in my opinion. It actually fixes the problem instead of masking it.Zr
Note that tableView:willSelectRowAtIndexPath: is called after highlighting the cell, which will produce a flicker. From Apple's description: This method is not called until users touch a row and then lift their finger; the row isn't selected until then, although it is highlighted on touch-down.Circumstantiality
I agree with @simpleBob. In order to use this solution, you need to also set cell.selectionStyle to UITableViewCellSelectionStyleNone on the unselectable rows. Otherwise it looks tacky.Trioxide
Then the best answer is still the accepted answer. You can just choose to do nothing in didSelectRowAtIndexPath because willSelectRowAtIndexPath will always be called anyway.Craftwork
worked great for me. for rowIsSelectable, i just did UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.selectionStyle != UITableViewCellSelectionStyleNone)Reply
E
154

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:.

Erythrite answered 1/5, 2009 at 19:54 Comment(3)
Kendall is right. Follow Sebastian Celis' answer to prevent didSelectRowAtIndexPath from being called in the first place. You should also set selectionStyle, though, to prevent the highlighting.Erythrite
yeah i suppose the Sebastian Celis' answer to be the correct answer, thanks DanielBastille
Storyboard fans using static TableViews can select a UITableViewCell and under Attributes Inspector can find the Selection Field and set it to NoneTemperament
C
17

use this:

cell.selectionStyle = UITableViewCellSelectionStyleNone;
Cheeks answered 20/8, 2010 at 11:49 Comment(3)
That only stops the selection color from showing, didSelectRowAtIndexPath is still called.Orman
This is also exactly the same answer as Daniel Dickison's to this question.Rubidium
Swift 2.2 UPDATE -- cell.selectionStyle = UITableViewCellSelectionStyle.NoneTartan
F
11

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

Fides answered 6/8, 2013 at 17:44 Comment(1)
Should have far more up votes. This is the correct answer, changing selection style so you can't see a cell is selected, disabling userinteraction so you can't tap on a cell or deselecting cells on selection are hacks.Otherwise
S
9

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!

Shanan answered 29/12, 2009 at 16:13 Comment(3)
Problem with this is that it makes it so all cells in the tableview are not selectable. If you want some cell to be selectable, and others to not be selectable, this won't do it. Sebastian's approach worked for me.Bibcock
True, allowsSelection = NO will disable selection for all cells.Shanan
Is there an equivalent property in Interface Builder for this property?Raccoon
D
8

Set cell.userInteractionEnabled = NO;

Diphase answered 9/11, 2010 at 17:28 Comment(1)
Wait, do it like that. Sort of. make 'FALSE' lower case and lose that crazy semicolon. That will swift it for you. ' FALSE' was evil and wrong, now 'NO' is evil and wrong. Until next week when 'false' is evil and wrong do it that way.Tonguetied
V
6

using IB is also an elegant way:

enter image description here

Violette answered 5/3, 2014 at 6:31 Comment(0)
P
3

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

Psychosomatics answered 1/5, 2009 at 17:49 Comment(1)
In that case, when a user touches the cell, it will be highlighted for a short time and then unhighlighted - but as I understand, the point is to completely stop it from highlighting.Adair
O
3

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;
}
Orman answered 8/11, 2010 at 3:44 Comment(0)
C
3

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.

Communicant answered 26/3, 2018 at 23:55 Comment(0)
Z
2

Swift 5

cell.isUserInteractionEnabled = false
Zingg answered 6/2, 2020 at 4:54 Comment(0)
S
1

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

Simonne answered 22/12, 2010 at 3:22 Comment(0)
C
0

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.

Croft answered 2/5, 2013 at 0:40 Comment(0)
G
0

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

Goyette answered 27/9, 2013 at 20:58 Comment(0)
M
0

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;
}
Madisonmadlen answered 25/3, 2015 at 12:51 Comment(0)
C
0

Swift 4:

In the cell class:

selectionStyle = .none
Corwin answered 26/4, 2018 at 0:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.