Home > database >  My function tableView.didSelectRow is not working
My function tableView.didSelectRow is not working

Time:11-09

I have a tableView with custom cells and my goal is to be able to click on one and print the number of the current row.

Right now when I click on a cell not happens and I can't understand why.

This is the code:

//MARK: - UITableDataSource
extension ChannelViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return groupResult.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let group = groupResult[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: K.cellContactNibName, for: indexPath) as! ContactCell
        cell.usernameLabel.text = group.name
        cell.messageLabel.text = group.recentMessage
        
        let storage = Storage.storage()
        let storageRef = storage.reference()
        
        // Reference to an image file in Firebase Storage
        let reference = storageRef.child("imagesFolder/\(group.image)")

        // UIImageView in your ViewController
        let imageView: UIImageView = cell.uImage

        // Placeholder image
        let placeholderImage = UIImage(named: "MeAvatar")

        // Load the image using SDWebImage
        imageView.sd_setImage(with: reference, placeholderImage: placeholderImage)
        
        cell.uImage.image = imageView.image
    
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("You selected cell #\(indexPath.row)!")
    }
}

and as you can see in the screen the single selection is enabled:

enter image description here

CodePudding user response:

change to

extension ChannelViewController: UITableViewDataSource, UITableViewDelegate

or move the method into another extension conformed to UITableViewDelegate

also, remember to set the tableView's delegate to the ChannelViewController

  • Related