Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
spase84 opened this issue Apr 4, 2018 · 2 comments
Assignees
Labels

Comments

@spase84
Copy link

spase84 commented Apr 4, 2018

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.

@wltrup
Copy link
Contributor

wltrup commented Jul 13, 2018

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 wltrup removed the bug label Jul 13, 2018
@wltrup
Copy link
Contributor

wltrup commented Jul 13, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants