How can I disable the UITableView selection?
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
How can I disable the UITableView selection?
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
Removing UITableViewCell Selection
You could try:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
if you just want the highlight to go away after selecting a cell. Unless I misunderstand your question.
Can I disable the UITableView selection of the individual cell?
cell.selectionStyle = UITableViewCellSelectionStyleNone;
or [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
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.
UITableview: How to Disable Selection for Some Rows but Not Others
You just have to put this code into cellForRowAtIndexPath
To disable the cell's selection property: (while tapping the cell)
cell.selectionStyle = UITableViewCellSelectionStyleNone;
To enable being able to select (tap) the cell: (tapping the cell)
// Default style
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
// Gray style
cell.selectionStyle = UITableViewCellSelectionStyleGray;
Note that a cell with selectionStyle = UITableViewCellSelectionStyleNone;
will still cause the UI to call didSelectRowAtIndexPath
when touched by the user. To avoid this, do as suggested below and set.
cell.userInteractionEnabled = NO;
instead. Also note you may want to set cell.textLabel.enabled = NO;
to gray out the item.
Remove the cell highlight color of UITableView
In Swift:
cell.selectionStyle = UITableViewCell.SelectionStyle.none
or simply:
cell.selectionStyle = .none
How to disable UITableViewCell highlighting?
Set UITableViewCell selection style none
cell.selectionStyle = .None
disable the uitableview highlighting but allow the selection of individual cells
You can just set the cell's selection style to "None" from Storyboard:
Or from code:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
For Swift 3:
cell.selectionStyle = UITableViewCellSelectionStyle.none
For Swift 4 & above:
cell.selectionStyle = .none
Swift 3: UITableViewCell disable selection style also disables selection
I came up with my own custom solution to the described problem:
within the awakeFromNib method of the cell i just created UIView instance like:
self.selectedBackgroundView = UIView()
without doing any more in that method. As an addition to solve my problem, i outsourced all of my ui design stuff to a custom method.
Then i overwrote the setHighlighted and setSelected methods:
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.setUIproperties()
}
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
super.setHighlighted(highlighted, animated: animated)
self.setUIproperties()
}
Why did i do this?
Cause overwriting is necessary to provide the custom design for your cell. Otherwise all of the backgrounds would've been set to white.
Overwriting setSelected isn't enough, cause when you long press the cell, it will blink and you see, that the highlighted state will also manipulate your ui.
I hope this helps to some of you.
Related Topics
Save Custom Objects into Nsuserdefaults
Didselectrowatindexpath Not Working, Swift 3
#Warning: C-Style For Statement Is Deprecated and Will Be Removed in a Future Version of Swift
Detecting Uibutton Pressed in Tableview: Swift Best Practices
Allow Only Numbers For Uitextfield Input
Make a Phone Call Programmatically
iOS Uitextview or Uilabel with Clickable Links to Actions
How to Use Facebook iOS Sdk on iOS 10
How to Correctly Linearize Depth in Opengl Es in Ios
Anyobject Not Working in Xcode8 Beta6
Detecting Sheet Was Dismissed on iOS 13
Navigation Bar With Uiimage For Title
Xcode 9 Error: "Iphone Has Denied the Launch Request"
Why Is Wkwebview Not Opening Links With Target="_Blank"
Importing Project-Swift.H into a Objective-C Class...File Not Found