Why would a table view not allow me to select its rows? I have programmatically set the isUserInteractionEnabled and allowsSelection properties to true and have set the table view cell's isUserInteractionEnabled property to true.
The table view is able to scroll, and I noticed that the row becomes selected when I tap on the label on the cell, but it does not become selected when I tap on the text view on the cell.
I am totally stumped on this one.
Here is my relevant code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.dataSource = self
tableView.delegate = self
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
tableView.allowsSelection = true
tableView.isUserInteractionEnabled = true
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: DatabaseNameStrings.recordTypeMessage, predicate: predicate)
privateDatabase.perform(query, inZoneWith: nil) {
records, error in
if error != nil {
print(error!.localizedDescription)
} else {
for record in records! {
let utiMessage = UTIMessage(ckRecord: record)
self.messages.append(utiMessage)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
// MARK: - UITableViewDataSource
extension CreateMessagesViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "messageCell", for: indexPath) as! MessageTableViewCell
let message = messages[indexPath.row]
cell.labelTitle.text = message.title
cell.textViewBody.text = message.body
return cell
}
}
// MARK: - UITableViewDelegate
extension CreateMessagesViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("didSelectRowAt")
}
}
This fixed it. I set the class property of the text view to this custom class:
Thank you everyone for your help.
class TextViewWithTouches: UITextView {
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
next?.touchesBegan(touches, with: event)
}
override func touchesMoved(_ touches: Set, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
next?.touchesMoved(touches, with: event)
}
override func touchesEnded(_ touches: Set, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
next?.touchesEnded(touches, with: event)
}
override func touchesCancelled(_ touches: Set, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
next?.touchesCancelled(touches, with: event)
}
}