[英]How to add Action for Static TableView Cell tap?
how can i respond to cell taps in static tableview?我如何响应静态 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 didSelectRowAt
没有被调用
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
.UITableViewController
的子类。tableView(_:didSelectRowAt:)
is a method of UITableViewDelegate
.tableView(_:didSelectRowAt:)
是UITableViewDelegate
一种方法。This can be done using a Static table in a UIContainerView
.这可以使用
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).当您使用
UIContainerView
它会创建一个embed segue... 这将触发prepare
调用,您可以在其中将 tableView 的委托设置为 self(如果需要)。
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
.请注意
EmbedTestViewController
prepare
func 中的注释和注释代码行。
If you run this with those lines commented, didSelectRowAt
will be called in MyStaticTableViewController
class.如果在注释了这些行的情况下运行它,将在
MyStaticTableViewController
类中调用didSelectRowAt
。
If you un-comment those lines, didSelectRowAt
will be called in EmbedTestViewController
class.如果取消注释这些行,将在
EmbedTestViewController
类中调用didSelectRowAt
。
EDIT编辑
Here is a full example: https://github.com/DonMag/ContainerTableView这是一个完整的示例: https : //github.com/DonMag/ContainerTableView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.