-1

I would like to make the cells of a tableView non-selectable but still allow scrolling. When I placed

 tableView.isUserInteractionEnabled = false 

which is recommended in some answers in viewDidLoad, it prevents selection but also prevents scrolling.

Adding:

 cell.selectionStyle = .none 

in cellforrowatindexpath as below not have any effect for me.

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { self.tableView.beginUpdates() self.tableView.endUpdates() if let cell = tableView.dequeueReusableCell(withIdentifier: "myMessageCell", for: indexPath) as? myMessageCell { cell.message = messages[indexPath.row] cell.selectionStyle = .none } return tableView.dequeueReusableCell(withIdentifier: "myMessageCell", for: indexPath) } 

Can anyone suggest how to prevent selection without preventing scrolling?

Thanks in advance for any suggestions.

7
  • 1
    What is happening when you select the cell now? The color changes? That should be fixed is you set the selectionStyle to .none Commented Sep 16, 2018 at 16:42
  • "did not have any effect for me"? What effect where you expecting? What happened? Commented Sep 16, 2018 at 16:44
  • The cell gets highlighted or not. Will add more code above. I was expecting it to not change color e.g. highlight. Commented Sep 16, 2018 at 16:46
  • 1
    Why self.tableView.beginUpdates(), self.tableView.endUpdates()? Commented Sep 16, 2018 at 16:51
  • Also, if you know reuseIdentifier "myMessageCell" will always return a myMessageCell, this is an appropriate place to use as! myMessageCell. There's probably an issue in your storyboard that force unwrapping will uncover. Commented Sep 16, 2018 at 16:53

2 Answers 2

2

Hope you are looking for this one:

tableView.allowsSelection = false 
Sign up to request clarification or add additional context in comments.

2 Comments

This did the job. Thx!
@user6631314 This will let you disable selection, but your cell data will go missing if you are still following the code for cellForRowAt that you have in your question.
1

The problem is you are not returning the same cell that you are dequeuing. Instead you are dequeuing another cell and returning it which has none of the properties that you have set.

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if let cell = tableView.dequeueReusableCell(withIdentifier: "myMessageCell", for: indexPath) as? myMessageCell { cell.message = messages[indexPath.row] cell.selectionStyle = .none return cell // Return the cell here instead } // return tableView.dequeueReusableCell(withIdentifier: "myMessageCell", for: indexPath) // Return another cell which has none of your properties set. return UITableViewCell() // return default cell in case your cell is not dequeued } 

3 Comments

The cells are returning the correct values. I don't think I can return a generic cell as it is customized.
@user6631314 try it. It has already been dequeued in the if let line.
@user6631314 if the first return which has the "myMessageCell" is dequeued properly, then the first return will be executed and the second dequeue will not be executed. The second return is a backup in case your "myMessageCell" dequeue fails. In which case it will display an empty cell instead of crashing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.