Middle
How to make it so that when tapping on one column, others are not highlighted?
sobes.tech AI
Answer from AI
You can create a custom UITableViewCell and override methods to manage the selection state.
Here's an example:
import UIKit
class CustomCell: UITableViewCell {
override func setSelected(_ selected: Bool, animated: Bool) {
// Do not call super.setSelected
// Manage appearance yourself if necessary
}
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
// Do not call super.setHighlighted
// Manage appearance yourself if necessary
}
}
In tableView(_:cellForRowAt:), you should use this custom class:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell else {
fatalError("The dequeued cell is not an instance of CustomCell.")
}
// Configure the cell
return cell
}
You can also set UITableViewCell.selectionStyle to .none:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)
cell.selectionStyle = .none // Disables the default highlight on tap
// Configure the cell
return cell
}