Why am I not able to select a row on my table view?

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")
        
    }
    
}

Accepted Reply

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)
            
        }

    }

Replies

ShinehahGnolaum
Can you share some snippets of your code regarding UITableView setup and cellForRowAtIndex method ?

    tableView.dataSource = self
    tableView.delegate = self
    
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 44
    
    tableView.allowsSelection = true
    tableView.isUserInteractionEnabled = true




    class MessageTableViewCell: UITableViewCell {


        @IBOutlet weak var labelTitle: UILabel!
        @IBOutlet weak var textViewBody: UITextView!
        
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
            
            self.isUserInteractionEnabled = true
            
        }
        
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
            
            // Configure the view for the selected state
        }


    }


    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
        
    }

The table view is scrolling just fine.

I was suddenly able to select a row -- the last row, and then I was not able to do that anymore, even after restarting the project.

You cannot select, what do you mean, what happens ?


You should show the complete code for the class with the table view.


Did you register the nib for the cell, with something like this in viewDidLoad


        let nib = UINib(nibName: "MessageTableViewCell", bundle: nil)   // if MessageTableViewCell is Nib name as well as class
        tableView.register(nib, forCellReuseIdentifier: "MessageTableViewCell")

I did not do that. I used a table view cell object on Interface Builder so I didn't have to do that.


When I tap on the table view, the row I tap would not be selected. The table view scrolls alright. I am able to select a row using code. I will post more of the code.

OK. I posted the relevant code in the main post.


I should add that the didSelectRowAt doesn't call.

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.

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)
            
        }

    }

If you post again on the forum, please describe the problem correctly.


You never mentionned that you have a custom UITextView class (TextViewWithTouches) and that the problem occurs only when you click on the textView.


So anyone that wants to help looses its time, by lack of appropriate information.


And finally, maybe you should wait a bit before posting, as at the end you always find the solution by yourself. Which is the best.

I had no idea what the problem was or what factors were involved. I had also posted on stackoverflow and a suggestion someone made led me to ask the questions that led to the solution. I wasn't using a custom UITextView class at the beginning. Using a custom UITextView class was what I did to solve the problem. At the beginning I didn't even notice that the problem happened only when I tapped on the text view. As I said at the beginning, I was stumped. I wasn't even sure what to include in the question. I checked out everything that commenters suggested to me, including your suggestion. Every effort anyone made to help me eventually led to a solution.


I'm trying my best to make it as easy on everyone as I can. I apologize for my silly mistakes. I will make every effort to do better.