Bocaxica
Bocaxica

Reputation: 4680

For a nested UITableView inside a UITableViewCell the didSelectRowAt indexPath: is not being called

I have a UITableView nested inside another UITableView. In the nested tableView, when the user taps on a cell, the cell does highlight, but the delegate method didSelectRowAt indexPath: is not being called.

Other delegate methods are being called, for example methods like willDisplay cell: forRowAt indexPath: or scrollViewDidScroll(_:). So this tells me that the delegates etc. are connected correctly.

In another part of the same application I am using the same kind of structure, UITableView inside another UITableView and there it works fine. I compared the two extensively, so far I haven't found the difference, and no clue why one should work and the other not!

Please see the implementation of the nested UITableViewCell. It would be nice if the console would log the line "touch detected". It does log the lines "scrolling" and "will display cell".

import UIKit

class TestTableViewCell: UITableViewCell {

}

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

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "FileCell")!
    }
}

extension TestTableViewCell: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("touch detected")
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        print("will display cell")
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("scrolling")
    }
}

Upvotes: 0

Views: 825

Answers (1)

Bocaxica
Bocaxica

Reputation: 4680

Solution was very simple at last. I had a UITapGestureRecognizer setup for the outer UITableView. I had this there to dismiss the keyboard on tap. After removing the UITapGestureRecognizer it started working fine.

I completely overlooked this, but it's solved now!

Upvotes: 1

Related Questions