-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.

user6631314
  • 1,751
  • 1
  • 13
  • 44
  • 1
    What is happening when you select the cell now? The color changes? That should be fixed is you set the `selectionStyle` to `.none` – Rakesha Shastri Sep 16 '18 at 16:42
  • _"did not have any effect for me"_? What effect where you expecting? What happened? – Ashley Mills Sep 16 '18 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. – user6631314 Sep 16 '18 at 16:46
  • 1
    Why `self.tableView.beginUpdates(), self.tableView.endUpdates()`? – Ashley Mills Sep 16 '18 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. – Ashley Mills Sep 16 '18 at 16:53
  • Ok. I am just learning Swift so still getting my head around optionals. Begin and endUpdates was an effort to get cells to autoresize based on content that is still unsolved. – user6631314 Sep 16 '18 at 16:57
  • For anyone visiting this page in the future, please note that my question is about Swift. The linked to question from 2015 provides answers for Objective-C – user6631314 Sep 16 '18 at 23:27

2 Answers2

2

Hope you are looking for this one:

tableView.allowsSelection = false
Muzahid
  • 5,072
  • 2
  • 24
  • 42
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
}
Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50
  • The cells are returning the correct values. I don't think I can return a generic cell as it is customized. – user6631314 Sep 16 '18 at 23:36
  • @user6631314 try it. It has already been dequeued in the if let line. – Rakesha Shastri Sep 17 '18 at 02:01
  • @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. – Rakesha Shastri Sep 17 '18 at 09:52