Reputation: 383
I have a UITableView inside my VC, and basically what I want is the first section of it be non-tappable. But I cannot use isUserInteractionEnabled
because I have UISwitch inside of each row of this section. Setting selectionStyle
to .none
changes nothing. I can only pick No Selection
in the interface inspector to disable those rows, but it disables the entire table. What should I do?
EDIT
Here's my custom cell class
class CustomCell: UITableViewCell {
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
if highlighted {
self.backgroundColor = ColorConstants.onTapColor
} else {
self.backgroundColor = .clear
}
}
override func setSelected(_ selected: Bool, animated: Bool) {
if selected {
self.backgroundColor = ColorConstants.onTapColor
} else {
self.backgroundColor = .clear
}
}
}
Upvotes: 2
Views: 2054
Reputation: 383
So, I found the reason why cells with selectionStyle
set to .none
got highlighted on tap. Because of me overriding setHighlighted
method (as shown in the question) of UITableViewCell
I added shouldHighlightRowAt
method like this:
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
if indexPath.section == 0 {
return false
} else {
return true
}
}
Thanks everybody for helping me
Upvotes: 1
Reputation: 829
You can set the selectionStyle
of all UITableViewCells
in the first section to .none
as follows:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "YOURIDENTIFIER")
if indexPath.section == 0 {
cell.selectionStyle = .none
} else {
cell.selectionStyle = .default
}
return cell
}
Then in your didSelectRowAtIndexPath()
method you can check if (indexPath.section != YOURSECTION)
:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
// DO NITHING
} else {
// DO WHATEVER YOU WANT TO DO WITH THE CELLS IN YOUR OTHER SECTIONS
}
}
Upvotes: 4
Reputation: 813
in cellForRowAt add cell.selectionStyle = .none
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexPath.section == desiredSection){
cell.selectionStyle = .none
return cell;
}
Upvotes: 0
Reputation: 12334
You will have to set the selection style per cell in code.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeue...
if indexPath.section == 0 {
cell.selectionStyle = .none
} else {
cell.selectionStyle = .default
}
return cell
}
Upvotes: 0