When you tap a row in a UITableView
, the row is highlighted and selected. Is it possible to disable this so tapping a row does nothing?
42 Answers
All you have to do is set the selection style on the UITableViewCell
instance using either:
Objective-C:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
or
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Swift 2:
cell.selectionStyle = UITableViewCellSelectionStyle.None
Swift 3 and 4.x:
cell.selectionStyle = .none
Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath:
in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.
More info here and here
-
123
-
74@TonyMillion it's also a bad idea if you want to use in-built editing controls, such as swipe-to-delete. If you set userInteractionEnabled to NO then the delete button does not respond to user touch events.– Carlos POct 25, 2011 at 23:52
-
49late to this thread, but instead of userInteraction, use cell.allowsSelection. That stops the cell's interaction methods but doesn't block the regular UIView responder stuff.– Chris CJul 10, 2012 at 0:17
-
4Setting cell.userInteractionEnable=No will not stop triggering a didSelectRowAtIndexPath.– user4951Oct 15, 2012 at 1:32
-
25I will up vote the answer, but just to be clear, the OP asked only for disabling "highlighting", no to disable entire cell interaction, so all this comments are offtopic. Apr 2, 2014 at 15:54
For me, the following worked fine:
tableView.allowsSelection = false
This means didSelectRowAt#
simply won't work. That is to say, touching a row of the table, as such, will do absolutely nothing. (And hence, obviously, there will never be a selected-animation.)
(Note that if, on the cells, you have UIButton
or any other controls, of course those controls will still work. Any controls you happen to have on the table cell, are totally unrelated to UITableView's ability to allow you to "select a row" using didSelectRowAt#
.)
Another point to note is that: This doesn't work when the UITableView
is in editing mode. To restrict cell selection in editing mode use the code as below:
tableView.allowsSelectionDuringEditing = false
-
6this finally worked for me, I tried to use cell.selectionStyle = UITableViewCellSelectionStyleNone; but it didn't work. One thing to note: it should be: tableView.allowsSelection = NO; not false. May 21, 2010 at 18:26
-
6I think, this is best option. Buttons on cells are active with this, but text are not active. Feb 10, 2013 at 12:56
-
545How is this the accepted answer and has this many up votes? It does not answer the question at all! You need to set the
selectionStyle
property on the cell:cell.selectionStyle = UITableViewCellSelectionStyle.None
in Swift, orcell.selectionStyle = UITableViewCellSelectionStyleNone
in Objective-C.– nodebaseSep 23, 2015 at 4:52 -
5
-
16There's a few ways to do this depending on what is desired. No selection style and no selection can both work, or can both fail. Here's an example. If you want to select the cell and have the
tableViewDidSelectRowAtIndexPath
to be called,cell.selectionStyle = .None
is desired. If you don't want or care for that delegate function to be called (ie having a textfield in a tableViewCell) settingtableView.allowsSelection = false
is just fine.– Made2kFeb 15, 2016 at 19:16
Because I've read this post recently and it has helped me, I wanted to post another answer to consolidate all of the answers (for posterity).
So, there are actually 5 different answers depending on your desired logic and/or result:
1.To disable the blue highlighting without changing any other interaction of the cell:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
I use this when I have a UIButton - or some other control(s) - hosted in a UITableViewCell and I want the user to be able to interact with the controls but not the cell itself.
NOTE: As Tony Million noted above, this does NOT prevent tableView:didSelectRowAtIndexPath:
. I get around this by simple "if" statements, most often testing for the section and avoiding action for a particular section.
Another way I thought of to test for the tapping of a cell like this is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// A case was selected, so push into the CaseDetailViewController
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.selectionStyle != UITableViewCellSelectionStyleNone) {
// Handle tap code here
}
}
2.To do this for an entire table, you can apply the above solution to each cell in the table, but you can also do this:
[tableView setAllowsSelection:NO];
In my testing, this still allows controls inside the UITableViewCell
to be interactive.
3.To make a cell "read-only", you can simply do this:
[cell setUserInteractionEnabled:NO];
4.To make an entire table "read-only"
[tableView setUserInteractionEnabled:NO];
5.To determine on-the-fly whether to highlight a cell (which according to this answer implicitly includes selection), you can implement the following UITableViewDelegate
protocol method:
- (BOOL)tableView:(UITableView *)tableView
shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
-
3If you load the cell design from a nib, the checkbox (in IB) will work just fine for #3 (and on tables for #4). For #4 vs #2, is the difference whether or not controls inside the cell will be interactive? Jun 10, 2011 at 22:23
-
4Yes. If you want to disable selection but allow UIButtons, etc... to continue to support user interaction, use #2. If you want a completely read-only cell, use #4. Of course, why you would have anything but UIViews or UILabels in a non-interactive cell is beyond me. Maybe somebody would want to disable all interaction while something else is occurring.– mbm29414Jun 10, 2011 at 23:45
-
10#4 is a terrible idea, doing this will also disable scrolling in the table view– simonApr 9, 2014 at 5:37
-
4@simon Actually, it's not a terrible idea; it's one option that may or may not be a good option depending on your circumstances. Also, #4 disables USER scrolling; you could still implement your own, if necessary. I agree #4 isn't useful MUCH, but it isn't a terrible idea.– mbm29414Apr 10, 2014 at 0:09
-
3For completeness could you please add a shout-out for
tableView:shouldHighlightRowAtIndexPath:
, which is generally (not always) my preferred approach.– BenjohnJul 21, 2015 at 13:55
To sum up what I believe are the correct answers based on my own experience in implementing this:
If you want to disable selection for just some of the cells, use:
cell.userInteractionEnabled = NO;
As well as preventing selection, this also stops tableView:didSelectRowAtIndexPath: being called for the cells that have it set. (Credit goes to Tony Million for this answer, thanks!)
If you have buttons in your cells that need to be clicked, you need to instead:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
and you also need to ignore any clicks on the cell in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
.
If you want to disable selection for the whole table, use:
tableView.allowsSelection = NO;
(Credit to Paulo De Barros, thanks!)
-
-
tableView.allowsSelection = NO;
doesn't allow buttons clicks inside cells... has something changed since? I know this post is old Jun 30, 2021 at 23:02 -
@WestonAMitchell You may want to check out the sentence beginning "If you have buttons in your cells that need to be clicked". You need to use the setSelectionStyle solution.– JosephHJul 1, 2021 at 21:42
-
@JosephH I had that set already. I actually found out that my issue was with my constraints using
self
of UITableViewCell to set constraints of its buttons, whilst I should have been using self.contentView. Also making sure that all views are vertically constrained from top of contentView, to bottom. Jul 1, 2021 at 21:57
As of iOS 6.0, UITableViewDelegate
has tableView:shouldHighlightRowAtIndexPath:
. (Read about it in the iOS Documentation.)
This method lets you mark specific rows as unhighlightable (and implicitly, unselectable) without having to change a cell's selection style, messing with the cell's event handling with userInteractionEnabled = NO
, or any other techniques documented here.
-
2Stumbled across this thread and I was shocked to see so many other answers prior to this one. This is without a doubt the proper way.– beebconMay 6, 2016 at 12:50
-
2This is indeed the correct and only answer that should be considered. Anything else mentioning userInteraction is completely incorrect.– mattyoheJun 9, 2016 at 21:40
You can also disable selection of row from interface builder itself by choosing NoSelection
from the selection
option(of UITableView Properties) in inspector pane as shown in the below image
-
2
-
1
EDIT: for newer Swift it is changed to:
cell.selectionStyle = .none
See this for more info: https://developer.apple.com/documentation/uikit/uitableviewcell/selectionstyle
In case anyone needs answer for Swift:
cell.selectionStyle = .None
-
1we can also use this cell.selectionStyle = UITableViewCellSelectionStyle.None– bablySep 9, 2015 at 9:47
-
This is exactly what I needed. I still wanted the didSelect... notification but without the visible indication of selection. Thanks! Jan 19, 2017 at 16:23
If you want selection to only flash, not remain in the selected state, you can call, in
didSelectRowAtIndexPath
the following
[tableView deselectRowAtIndexPath:indexPath animated:YES];
so it will flash the selected state and revert.
-
3With this approach, the row instantly highlights and then fades out over a half-second. This is how Apple styles their Settings menus, so it looks good to me. Sep 3, 2015 at 6:08
From the UITableViewDelegate
Protocol you can use the method willSelectRowAtIndexPath
and return nil
if you don't want the row selected.
In the same way the you can use the willDeselectRowAtIndexPath
method and return nil
if you don't want the row to deselect.
-
6I don't like this method because it still shows the cell in its highlighted state until touch up.– kbanmanFeb 14, 2010 at 8:25
-
2
This is what I use ,in cellForRowAtIndexPath
write this code.:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
1- All you have to do is set the selection style on the UITableViewCell
instance using either:
Objective-C:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
or
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Swift 2:
cell.selectionStyle = UITableViewCellSelectionStyle.None
Swift 3:
cell.selectionStyle = .none
2 - Don't implement -tableView:didSelectRowAtIndexPath:
in your table view delegate
or explicitly exclude the cells you want to have no action if you do implement it.
3 - Further,You can also do it from the storyboard. Click the table view cell and in the attributes inspector under Table View Cell, change the drop down next to Selection to None.
4 - You can disable table cell highlight using below code in (iOS) Xcode 9 , Swift 4.0
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "OpenTbCell") as! OpenTbCell
cell.selectionStyle = .none
return cell
}
Objective-C:
Below snippet disable highlighting but it also disable the call to
didSelectRowAtIndexPath
. So if you are not implementingdidSelectRowAtIndexPath
then use below method. This should be added when you are creating the table. This will work on buttons andUITextField
inside the cell though.self.tableView.allowsSelection = NO;
Below snippet disable highlighting and it doesn't disable the call to
didSelectRowAtIndexPath
. Set the selection style of cell to None incellForRowAtIndexPath
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Below snippet disable everything on the cell. This will disable the interaction to
buttons
,textfields
:self.tableView.userInteractionEnabled = false;
Swift:
Below are the Swift
equivalent of above Objective-C
solutions:
Replacement of First Solution
self.tableView.allowsSelection = false
Replacement of Second Solution
cell?.selectionStyle = UITableViewCellSelectionStyle.None
Replacement of Third Solution
self.tableView.userInteractionEnabled = false
Try to type:
cell.selected = NO;
It will deselect your row when needed.
In Swift3 ...
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let r = indexPath.row
print("clicked .. \(r)")
tableView.cellForRow(at: indexPath)?.setSelected(false, animated: true)
}
Swift 3,4 and 5
Better practice, write code in UITableViewCell
For example, you have UITableViewCell
with the name MyCell
,
In awakeFromNib
just write self.selectionStyle = .none
Full example:
class MyCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
self.selectionStyle = .none
}
}
I've been battling with this quite profusely too, having a control in my UITableViewCell
prohibited the use of userInteractionEnabled
property. I have a 3 cell static table for settings, 2 with dates, 1 with an on/off switch. After playing about in Storyboard/IB i've managed to make the bottom one non-selectable, but when you tap it the selection from one of the top rows disappears. Here is a WIP image of my settings UITableView:
If you tap the 3rd row nothing at all happens, the selection will stay on the second row. The functionality is practically a copy of Apple's Calendar app's add event time selection screen.
The code is surprisingly compatible, all the way down to IOS2 =/:
- (NSIndexPath *)tableView: (UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 2) {
return nil;
}
return indexPath;
}
This works in conjunction with setting the selection style to none, so the cell doesn't flicker on touch down events
We can write code like
cell.selectionStyle = UITableViewCellSelectionStyleNone;
but when we have custom cell xib above line give warning at that time for
custom cell xib
we need to set selection style None from the interface builder
You just have to put this code into cellForRowAtIndexPath
To disable the cell's selection property:(While tapping the cell).
cell.selectionStyle = UITableViewCellSelectionStyle.None
-
Thank you, I have an image in my cell and a label above it. I've set the label background, but everytime I'd click the background would disappear, this has solved it.– DarkoAug 29, 2016 at 19:01
From UITableViewDataSource Protocol, inside method cellForRowAt
add:
let cell = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL_IDENTIFIER", for: indexPath)
cell.selectionStyle = .none
return cell
OR
You can goto Storyboard > Select Cell > Identity Inspector > Selection and select none from dropdown.
I am using this, which works for me.
cell?.selectionStyle = UITableViewCellSelectionStyle.None
try this
cell.selectionStyle = UITableViewCellSelectionStyleNone;
and
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
and you can also set selection style using interfacebuilder.
While this is the best and easiest solution to prevent a row from showing the highlight during selection
cell.selectionStyle = UITableViewCellSelectionStyleNone;
I'd like to also suggest that it's occasionally useful to briefly show that the row has been selected and then turning it off. This alerts the users with a confirmation of what they intended to select:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
...
}
Disable selection for all UITableViewCells in the UITableView
tableView.allowsSelection = false
Disable selection for specific UITableViewCells
cell.selectionStyle = UITableViewCell.SelectionStyle.none
To disable the highlighting of the UItableviewcell
cell.selectionStyle = UITableViewCellSelectionStyleNone;
And should not allow the user to interact with the cell.
cell.userInteractionEnabled = NO;
You Can also set the background color to Clear to achieve the same effect as UITableViewCellSelectionStyleNone
, in case you don't want to/ can't use UITableViewCellSelectionStyleNone
.
You would use code like the following:
UIView *backgroundColorView = [[UIView alloc] init];
backgroundColorView.backgroundColor = [UIColor clearColor];
backgroundColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView: backgroundColorView];
This may degrade your performance as your adding an extra colored view to each cell.
You can also do it from the storyboard. Click the table view cell and in the attributes inspector under Table View Cell, change the drop down next to Selection to None.
You can use :
cell.selectionStyle = UITableViewCellSelectionStyleNone;
in the cell for row at index path method of your UITableView.
Also you can use :
[tableView deselectRowAtIndexPath:indexPath animated:NO];
in the tableview didselectrowatindexpath method.
-
It's now
cell.selectionStyle = UITableViewCellSelectionStyle.none
in Swift 3.– DanJan 25, 2018 at 15:58
selection
, and of coursehighlighting
gets disabled with that. Many high voted answers explain how todisable selection
and does not address disablinghighlighting
. To disable only highlighting usecell.selectionStyle = .None
or go tostoryboard / cell / Selection = None