Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a cell on a UITableView not selectable?

I have a cell that I am inserting to the top of a UITableView. How can I make sure that when the user clicks on the cell, it doesn't show the blue selected indicator?

like image 239
Sheehan Alam Avatar asked Apr 14 '10 22:04

Sheehan Alam


People also ask

How do I turn off cell selection?

You need to set the selectionStyle property on the cell: cell. selectionStyle = UITableViewCellSelectionStyle. None in Swift, or cell.

How can we use a reusable cell in UITableView?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.


1 Answers

To remove the ability of selecting any table cells, or particular table cells based on the row index, use willSelectRowAt

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {           return nil     } 

To simply remove the UI effect of selecting the element, set the selection style of the UITableViewCell to UITableViewCellSelectionStyleNone

Swift 5:

selectionStyle = .none 
like image 118
John Wang Avatar answered Dec 07 '22 10:12

John Wang