0

I make tableView and collectionView in my ViewController and was set the Delegate and DataSource to my ViewController with extension or include the class.

The cell already show correctly on both of them, I haven't any UITapGestureRecognizer in my Class but when I want to tap on my Cell the didSelectRowAt not called.

I've added canEditRowAt and It's work but not with didSelectRowAt

This is my CollectionView

This is my TableView with edit action and it works

This is my ViewController Scene

My TableView already set :

- UserInteractionEnable = true 
- Selection = Single Selection

My Xib Files :

- UserInteractionEnable = true 

This is my class :

class ProfileVC: UIViewController {

    @IBOutlet weak var profileTableView: UITableView!

    var profileTitle = DataService.instance.profileTitle

    override func viewDidLoad() {
        super.viewDidLoad()
        configureTableView()
    }

    func configureTableView() {
        let nib = UINib(nibName: "ProfileCell", bundle: nil)
        profileTableView.register(nib, forCellReuseIdentifier: "ProfileCell")
        profileTableView.delegate = self
        profileTableView.dataSource = self
        profileTableView.allowsSelection = true
    }

}

extension ProfileVC: UITableViewDelegate, UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return profileTitle.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell", for: indexPath) as? ProfileCell else { return ProfileCell() }
        cell.configureCell(withTitle: profileTitle[indexPath.row])
        return cell
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Selected")
    }

}

This is my ProfileCellClass :

class ProfileCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    func configureCell(withTitle title: String){
        self.titleLabel.text = title
    }   
}

This my .xib file details

Thank you.

2 Answers2

0

If your table view is in editing mode like

tableView.setEditing(true, animated: false)

you need to set

tableView.allowsSelectionDuringEditing = true
0

In your ConfigureTableView method register your TableViewCell Class as well.

profileTableView.register(ProfileCell.self, forCellReuseIdentifier: "ActivityImageCell")
profileTableView.register(UINib(nibName: "ProfileCell", bundle: nil), forCellReuseIdentifier: "ProfileCell")

This could be a reason

Muhammad Nayab
  • 1,612
  • 14
  • 14