how can i respond to cell taps in static tableview? Is there a way to link it with code? I know that i can use a dynamic one but isn't it possible with a static one?
didSelectRowAt
doesn't get called
Code for Selecting not working
class SettingsVC: UITableViewController, UITabBarControllerDelegate {
// MARK: - View lifecycle
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("SELECTED") // not get called
}
}
You need to either:
UITableViewController
.tableView(_:didSelectRowAt:)
is a method of UITableViewDelegate
.This can be done using a Static table in a UIContainerView
.
Here is the Storyboard setup:
and how it looks at run-time:
When you use a UIContainerView
it creates an embed segue... and that will trigger a prepare
call where you can set the tableView's delegate to self (if desired).
Here's the code:
import UIKit
class EmbedTestViewController: UIViewController, UITableViewDelegate {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// When using UIContainerView, prepare for segue will be called on load
// un-comment this block to use SELF as the delegate
// for the tableView in the embedded tableViewController
//if let vc = segue.destination as? MyStaticTableViewController {
// vc.tableView.delegate = self
//}
}
// this will only be called if .delegate is set as shown above
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("didSelectRowAt called in \"Parent\" for indexPath:", indexPath)
}
}
class MyStaticTableViewController: UITableViewController {
// this will NOT be called if .delegate is set as the "parent" view controller
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("didSelectRowAt called in \"Table View Controller\" for indexPath:", indexPath)
}
}
Note the comments and commented lines of code in the prepare
func in EmbedTestViewController
.
If you run this with those lines commented, didSelectRowAt
will be called in MyStaticTableViewController
class.
If you un-comment those lines, didSelectRowAt
will be called in EmbedTestViewController
class.
EDIT
Here is a full example: https://github.com/DonMag/ContainerTableView
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.