disable the uitableview highlighting but allow the selection of individual cells
Asked Answered
P

12

86

when you tap on a cell the row gets selected and highlighted.Now what i want to do is disable the highlighting but allow the selection.Is there a way around it.There is question that answers this but it disables both the selection and highlighting.

Poppyhead answered 14/5, 2015 at 9:12 Comment(1)
cell.selectionStyle = UITableViewCellSelectionStyleNone; add this in your cellForRowAtIndexPath methodLisk
E
187

You can just set the cell's selection style to "None" from Storyboard:

See here

Or from code:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

For Swift 3:

cell.selectionStyle = UITableViewCellSelectionStyle.none

For Swift 4 & above:

cell.selectionStyle = .none
Ernie answered 14/5, 2015 at 9:16 Comment(3)
When I select to "none" I lose ability to work of of highlighted state. Can I make the background none/white without loosing that state?Creative
In which function would this go? I'm guessing "cellForRowAt"?Illa
You should do it in the cell not the tableviewLunisolar
P
27

Change UITableViewCell's selectedBackgroundView color to transparent.

    let clearView = UIView()
    clearView.backgroundColor = UIColor.clearColor() // Whatever color you like
    UITableViewCell.appearance().selectedBackgroundView = clearView

or to set for a specific cell:

cell.backgroundView = clearView

Pestilent answered 14/5, 2015 at 9:25 Comment(5)
This is the best options, since cell.selectionStyle = UITableViewCellSelectionStyleNone sometimes breaks something deeply inside Apples animation logic.Teyde
then use instead ..UITableViewCell.appearance().selectionStyle = .NonePood
This is nice to use if you have selectable things within the cell.Laughter
Excellent solution. Putting this code in the cell's awakeFromNib() function works a treatCobber
This option makes table view cell separator line not seen.Charlatan
D
8

Try setting cell selection style to None -

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

This will solve your problem

Dunbar answered 14/5, 2015 at 9:16 Comment(0)
H
8

For Swift 3.0:

cell.selectionStyle = .none
Harman answered 19/7, 2017 at 16:9 Comment(0)
D
6

For Swift:

UITableViewCell.selectionStyle = UITableViewCellSelectionStyle.None;

Or when you subclass a cell in swift:

class CustomCell : UITableViewCell {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        selectionStyle = .None
    }

}
Delagarza answered 20/7, 2016 at 13:40 Comment(0)
I
6

in swift 3

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
}
Isoniazid answered 16/10, 2017 at 7:46 Comment(0)
D
2

To add a custom color use the below code. And to make it transparent use alpha: 0.0

cell.selectedBackgroundView = UIView(frame: CGRect.zero)
cell.selectedBackgroundView?.backgroundColor = UIColor(red:0.27, green:0.71, blue:0.73, alpha:1.0)

If you use custom color and want to give it rounded corner look use:

cell.layer.cornerRadius = 8

Also, use this for better animation and feel

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    tableView.deselectRow(at: indexPath, animated: true)
}
Dowdy answered 5/2, 2018 at 12:38 Comment(0)
C
1

For those looking for a programmatic way in Swift 3

cell.selectionStyle = UITableViewCellSelectionStyle.none

Campball answered 17/10, 2016 at 10:9 Comment(0)
J
1

You can set the selection property of the cell itself in the storyboard enter image description here

Johnsiejohnson answered 21/4, 2018 at 22:57 Comment(0)
D
0

For Swift 5 the best way is:

cell.selectionStyle = .none
Didier answered 29/9, 2016 at 10:17 Comment(0)
B
0

For Objc:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

- (void)viewDidLoad {
  [super viewDidLoad];
  _tableView.allowsSelection = YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    .. .. .. .. 
   [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
   . . . . .. 
}
Balas answered 14/7, 2017 at 6:50 Comment(0)
S
-1

Here is the solution for swift 3,works even in editing mode

cell.selectionStyle = .gray
cell.selectedBackgroundView = {

    let colorView = UIView()
    colorView.backgroundColor = UIColor.black.withAlphaComponent(0.0)
    //change the alpha value or color to match with you UI/UX

    return colorView
}()
Sciatica answered 25/10, 2017 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.