DrawerKit
DrawerKit copied to clipboard
Presented UIViewController with UITableView, connected as outlet don't catch row selection.
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.
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.
Are you sure you set the delegate outlet of the table view to the presented view controller?
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)")
}
}
// -------