DrawerKit icon indicating copy to clipboard operation
DrawerKit copied to clipboard

Presented UIViewController with UITableView, connected as outlet don't catch row selection.

Open spase84 opened this issue 6 years ago • 2 comments

I've added UITableView to presented UIViewController, but when presented the delegate method didSelectRowAt doesn't called when i tap on row.

i have tried reenable userInteraction on table on ViewWillAppear method of presented VC.

spase84 avatar Apr 04 '18 11:04 spase84

Hello @spase84, and thank you for your interest in DrawerKit.

I tried to replicate the problem you described but it worked fine for me. All I did was:

  • I took the demo app code as is then replaced the image view with a table view
  • added an outlet for the table view to the presented view controller
  • set the data source and delegate outlets of the table view to the presented view controller
  • made the presented view controller conform to the data source and delegate protocols
  • implemented didSelectRowAt: to print the row number

As I said, it just worked. See gif below.

jul-13-2018 15-35-41

Are you sure you set the delegate outlet of the table view to the presented view controller?

wltrup avatar Jul 13 '18 14:07 wltrup

Here are the changes to PresentedViewController which I described above. Look for blocks of code marked with a // new code comment.

class PresentedViewController: UIViewController {
    private var notificationToken: NotificationToken!

    @IBOutlet weak var presentedView: PresentedView!
    @IBOutlet weak var tableView: UITableView! // new code

    @IBAction func dismissButtonTapped() {
        dismiss(animated: true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // new code
        tableView.delegate = self
        tableView.dataSource = self
        // --------

        self.notificationToken = NotificationCenter.default
            .addObserver(name: DrawerNotification.drawerExteriorTappedNotification) {
            (notification: DrawerNotification, object: Any?) in
            switch notification {
            case .drawerExteriorTapped:
                print("drawerExteriorTapped")
            default:
                break
            }
        }
    }

    @IBAction func unwindFromModal(with segue: UIStoryboardSegue) {}
}

// new code
extension PresentedViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // Generally, one should dequeue cells rather than create a new one every time this
        // function is called but, for this quick test, it's ok to do it this way.
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        cell.textLabel?.text = "Row \(indexPath.row + 1)"
        return cell
    }
}

extension PresentedViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        print("Selected row: \(indexPath.row + 1)")
    }
}
// -------

wltrup avatar Jul 13 '18 14:07 wltrup