1224

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?

Malik
  • 3,543
  • 1
  • 18
  • 32
davidmytton
  • 35,554
  • 36
  • 83
  • 91
  • 21
    The original question has a bad title. OP want's primarily to disable `selection`, and of course `highlighting` gets disabled with that. Many high voted answers explain how to `disable selection`and does not address disabling `highlighting`. To disable only highlighting use `cell.selectionStyle = .None` or go to `storyboard / cell / Selection = None` – Andrej Sep 07 '17 at 10:01
  • Please update either the title or the post. I'm confused. And based on this question, I have no idea what provided answers are meant to do. – Daniel Springer Jun 21 '18 at 08:41

41 Answers41

1988

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

Sunil Targe
  • 7,009
  • 5
  • 42
  • 71
Martin Gordon
  • 35,754
  • 6
  • 55
  • 53
  • 3
    @Tony That does work very well, thanks! How about posting it as an answer so we can upvote it to the top? – JosephH May 27 '11 at 14:29
  • 122
    @Tony It's bad idea if you have a UITextField inside the cell. – Aniruddh Sep 18 '11 at 13:53
  • 73
    @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 P Oct 25 '11 at 23:52
  • 47
    late 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 C Jul 10 '12 at 00:17
  • 1
    in http://stackoverflow.com/questions/12884691/what-are-the-possible-reasons-why-voidtableviewuitableview-tableview-dids/12886017#12886017 setting the cell.userInteractionEnabled = NO actually makes the didSelectRowAtIndexPath easier to trigger. – user4951 Oct 15 '12 at 01:19
  • 4
    Setting cell.userInteractionEnable=No will not stop triggering a didSelectRowAtIndexPath. – user4951 Oct 15 '12 at 01:32
  • 1
    Indeed, I cannot select the cell (didSelectRowAtIndexPath not triggered) after setting userInteractionEnable = NO. – Yunus Nedim Mehel Nov 16 '12 at 16:01
  • 1
    @TonyMillion Be aware that if you use `userInteractionEnabled` on a `UITableViewCell`, the textLabel's color will change to grey, and you CAN NOT prevent that. – Berik Mar 25 '13 at 13:23
  • 1
    In XCode, you could select the table view, and in the attributes inspector set "Selection" to "No Selection" – Luis Artola Jan 31 '14 at 09:24
  • 25
    I 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. – Frederic Yesid Peña Sánchez Apr 02 '14 at 15:54
  • 2
    Just to add a thing. In swift the one suggested become: cell.selectionStyle = UITableViewCellSelectionStyle.None – Nicholas Mar 13 '15 at 21:15
  • 2
    @TonyMillion it's also a bad idea if you want enable copying of a UILabel's text inside the cell. – Steve Moser Jul 13 '15 at 15:37
  • 1
    iOS 9: seems `cell.allowsSelection` is no longer available. Also note that disabling `userInteraction` makes it impossible to change `cell.textLabel.textColor` – Cbas Mar 11 '16 at 08:57
  • Put this code in `cellForRowAtIndexPath`. In `init`, it didn't work. – Colas Apr 22 '16 at 10:13
  • `cell.selectionStyle = .None` Its enough for me. – iDeveloper Sep 28 '16 at 11:06
  • **swift4.2** in `func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {` cell?.selectionStyle = UITableViewCell.SelectionStyle.none – uplearnedu.com Mar 26 '19 at 08:15
  • Hmm something fishy is going on here – Suh Fangmbeng Feb 14 '20 at 14:32
633

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 
Let's_Create
  • 1,573
  • 2
  • 8
  • 27
  • 6
    this 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. – tony.tc.leung May 21 '10 at 18:26
  • 6
    I think, this is best option. Buttons on cells are active with this, but text are not active. – Fahim Parkar Feb 10 '13 at 12:56
  • 537
    How 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, or `cell.selectionStyle = UITableViewCellSelectionStyleNone` in Objective-C. – nodebase Sep 23 '15 at 04:52
  • 2
    Worked well for me, I can still interact with textfields, picker etc in my cells – Kitson Oct 12 '15 at 23:36
  • 3
    Yes, this answer is the correct one for the given question. OPs wants to disable selection for the cell. – mcatach Dec 23 '15 at 17:59
  • 5
    This works for me in swift 2. `tableView.allowSelection = false ` – Khanad Feb 14 '16 at 18:21
  • 16
    There'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) setting `tableView.allowsSelection = false` is just fine. – Made2k Feb 15 '16 at 19:16
  • 3
    it is Google's fault for navigating users when searching 'how to disable uitableview didSelectRowAtIndexPath background color'. So blame it on Google – cmario Apr 20 '16 at 10:51
  • 2
    100% wrong answer. This won't disable highlighting, this will disable selection completely – aryaxt Jun 14 '16 at 16:01
  • 2
    Bad solution for me - disables selection. Com'on guys. – Alon_T Jun 17 '16 at 21:52
  • 2
    @nodebase how come others say this *does* work? I'm confused – Honey Dec 26 '16 at 03:35
  • 2
    This ended up working for me `tableView.allowsSelection = false` in the viewWillAppear method. – icekomo Jan 23 '17 at 23:25
  • **swift4.2** in `func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {` cell?.selectionStyle = UITableViewCell.SelectionStyle.none – uplearnedu.com Mar 26 '19 at 08:14
357

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
Community
  • 1
  • 1
mbm29414
  • 11,300
  • 6
  • 52
  • 82
  • 3
    If 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? – lilbyrdie Jun 10 '11 at 22:23
  • 4
    Yes. 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. – mbm29414 Jun 10 '11 at 23:45
  • @mbm30075 #1 the first one seems to best answer of all the 4 options – NNikN Jan 01 '13 at 08:01
  • This is the most complete answer! – Giovanni Jan 01 '13 at 08:06
  • @andyPaul It depends on what you're trying to achieve. I often use #1 when I want to preserve user interaction, but that's not always that case. Thanks! – mbm29414 Jan 02 '13 at 13:06
  • 8
    #4 is a terrible idea, doing this will also disable scrolling in the table view – simon Apr 09 '14 at 05: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. – mbm29414 Apr 10 '14 at 00:09
  • Very good and complete answer! Saved me a lot of issues I had with a Button and TextField inside my cell – iOS-Coder Mar 31 '15 at 09:28
  • 3
    For completeness could you please add a shout-out for [`tableView:shouldHighlightRowAtIndexPath:`](http://stackoverflow.com/a/13167946/2547229), which is _generally_ (not always) my preferred approach. – Benjohn Jul 21 '15 at 13:55
88

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!)

JosephH
  • 36,107
  • 19
  • 126
  • 149
56

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.

cbowns
  • 5,932
  • 4
  • 41
  • 63
  • 2
    Stumbled 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. – beebcon May 06 '16 at 12:50
  • 2
    This is indeed the correct and only answer that should be considered. Anything else mentioning userInteraction is completely incorrect. – mattyohe Jun 09 '16 at 21:40
51

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

UITableView Inspector

vignesh kumar
  • 2,260
  • 2
  • 22
  • 37
47

FIXED SOLUTION FOR SWIFT 3

cell.selectionStyle = .none
Oscar Falmer
  • 1,591
  • 1
  • 20
  • 34
MANISH PATHAK
  • 2,425
  • 3
  • 24
  • 28
36

In case anyone needs answer for Swift:

cell.selectionStyle = .None
Aks
  • 7,593
  • 5
  • 35
  • 36
36

In your UITableViewCell's XIB in Attribute Inspector set value of Selection to None.

enter image description here

Mohammad Zaid Pathan
  • 14,352
  • 6
  • 84
  • 112
  • This is exactly what I needed. I still wanted the didSelect... notification but without the visible indication of selection. Thanks! – Timothy Tripp Jan 19 '17 at 16:23
35

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.

Chris Fox
  • 601
  • 5
  • 10
  • 3
    With 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. – VinceFior Sep 03 '15 at 06:08
30

This is what I use ,in cellForRowAtIndexPath write this code.:

cell.selectionStyle = UITableViewCellSelectionStyleNone;
Community
  • 1
  • 1
Ahsan Ebrahim
  • 1,662
  • 1
  • 17
  • 26
29

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.

Paras Joshi
  • 19,954
  • 11
  • 54
  • 69
user41806
  • 539
  • 4
  • 6
25

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


}
heckj
  • 6,120
  • 2
  • 31
  • 45
parvind
  • 797
  • 10
  • 21
20

Objective-C:

  1. Below snippet disable highlighting but it also disable the call to didSelectRowAtIndexPath. So if you are not implementing didSelectRowAtIndexPath then use below method. This should be added when you are creating the table. This will work on buttons and UITextField inside the cell though.

    self.tableView.allowsSelection = NO;
    
  2. Below snippet disable highlighting and it doesn't disable the call to didSelectRowAtIndexPath. Set the selection style of cell to None in cellForRowAtIndexPath

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
  3. 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:

  1. Replacement of First Solution

    self.tableView.allowsSelection = false
    
  2. Replacement of Second Solution

    cell?.selectionStyle = UITableViewCellSelectionStyle.None
    
  3. Replacement of Third Solution

    self.tableView.userInteractionEnabled = false
    
M Reza
  • 13,860
  • 13
  • 53
  • 64
Arun Gupta
  • 2,482
  • 1
  • 16
  • 35
19

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)
}
Fattie
  • 30,632
  • 54
  • 336
  • 607
Denis Kutlubaev
  • 13,009
  • 6
  • 76
  • 67
14

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:

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

Yarek T
  • 9,132
  • 2
  • 25
  • 34
11

You just have to put this code into cellForRowAtIndexPath

To disable the cell's selection property:(While tapping the cell).

cell.selectionStyle = UITableViewCellSelectionStyle.None
Gaurav Patel
  • 534
  • 1
  • 5
  • 19
  • 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. – Darko Aug 29 '16 at 19:01
11

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

iDhaval
  • 7,806
  • 2
  • 18
  • 30
10

try this

cell.selectionStyle = UITableViewCellSelectionStyleNone;

and

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

and you can also set selection style using interfacebuilder.

Deepak Swami
  • 3,728
  • 1
  • 27
  • 46
v_1
  • 717
  • 7
  • 18
10

I am using this, which works for me.

cell?.selectionStyle = UITableViewCellSelectionStyle.None
josliber
  • 41,865
  • 12
  • 88
  • 126
priyanka gautam
  • 367
  • 3
  • 15
8

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.

José
  • 2,544
  • 24
  • 38
Abdul Karim Khan
  • 2,387
  • 16
  • 23
7

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];
...
}
Steve Barden
  • 496
  • 1
  • 5
  • 14
7

Directly disable highlighting of TableViewCell into storyboard

enter image description here

Pang
  • 8,605
  • 144
  • 77
  • 113
Kamani Jasmin
  • 575
  • 6
  • 10
6

To disable the highlighting of the UItableviewcell

cell.selectionStyle = UITableViewCellSelectionStyleNone;

And should not allow the user to interact with the cell.

cell.userInteractionEnabled = NO;
Sharme
  • 625
  • 2
  • 10
  • 28
6

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.

virindh
  • 3,685
  • 3
  • 22
  • 47
6

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.

Sangram Shivankar
  • 3,245
  • 3
  • 22
  • 36
AS-iOS
  • 4,183
  • 3
  • 18
  • 30
  • It's now `cell.selectionStyle = UITableViewCellSelectionStyle.none` in Swift 3. – Dan Jan 25 '18 at 15:58
6

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.

Cindy
  • 324
  • 2
  • 3
5

You can use this

cell.selectionStyle = UITableViewCellSelectionStyleNone;
iEinstein
  • 2,094
  • 1
  • 18
  • 32
5

The best solution would be Making The selection Style None

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

However, Here we are considering the fact that there are no custom images used for selected state.

Sangram Shivankar
  • 3,245
  • 3
  • 22
  • 36
Harini
  • 263
  • 1
  • 5
  • 14
5

You can use ....

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Mak083
  • 1,120
  • 1
  • 13
  • 33
5
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Fahim Parkar
  • 28,922
  • 40
  • 153
  • 260
iDeveloper
  • 213
  • 2
  • 5
5
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:NO];
[cell setHighlighted:NO animated:NO];

Happy coding !!!

Andrew T.
  • 4,592
  • 7
  • 38
  • 55
5

Swift Solution w/ Custom Cell:

import Foundation

class CustomTableViewCell: UITableViewCell
{
  required init(coder aDecoder: NSCoder)
  {
    fatalError("init(coder:) has not been implemented")
  }

  override init(style: UITableViewCellStyle, reuseIdentifier: String?)
  {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    self.selectionStyle = UITableViewCellSelectionStyle.None
  } 
}
Zorayr
  • 20,232
  • 5
  • 111
  • 102
5

You can use selectionStyle property of UITableViewCell

 cell.selectionStyle = UITableViewCellSelectionStyleNone;

Or

 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Also, do not implement below delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ... }

If you have created Xib/Storyboard file then you can change setUserInteractionEnabled property of tableview to No by unchecking it. This will make your tableview to Read-Only.

Jayprakash Dubey
  • 32,447
  • 16
  • 161
  • 169
3

You can disable table cell highight 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


}
praful argiddi
  • 804
  • 1
  • 6
  • 11
3

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
    }
    
}
Rashid Latif
  • 1,388
  • 14
  • 18
3

Disable selection for all UITableViewCells in the UITableView

tableView.allowsSelection = false

Disable selection for specific UITableViewCells

cell.selectionStyle = UITableViewCell.SelectionStyle.none
testing
  • 61
  • 7
2

The better approach will be:

cell.userInteractionEnabled = NO;

This approach will not call didSelectRowAtIndexPath: method.

Aniruddh
  • 7,398
  • 1
  • 23
  • 43
2

At least as of iOS 6, you can override methods in your custom cell to prevent the blue highlight. No other interaction is disabled or affected. All three must be overridden.

- (void) setHighlighted:(BOOL)highlighted
{
}

- (void) setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
}

- (void) setSelected:(BOOL)selected animated:(BOOL)animated
{
}
ashevin
  • 124
  • 3
1

Very simple stuff. Before returning the tableview Cell use the style property of the table view cell.

Just write this line of code before returning table view cell
cell.selectionStyle = .none

mate00
  • 2,181
  • 5
  • 21
  • 31
Muhammad Ahmad
  • 346
  • 4
  • 9
0

Scenario - 1

If you don't want selection for some specific cells on the tableview, you can set selection style in cellForRow function for those cells.

Objective-C

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Swift 4.2

cell.selectionStyle = .none

Scenario - 2

For disabling selection on the whole table view :

Objective-C

self.tableView.allowsSelection = false;

Swift 4.2

self.tableView.allowsSelection = false
vrat2801
  • 418
  • 2
  • 13