swift - TableView, didSelectRowAt not called
问题描述
What am I doing wrong? The declaration does not work.
its my code.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext =
appDelegate.persistentContainer.viewContext
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: "Waypoints")
do {
waypoint = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
extension ViewControllerWaypoints: UITableViewDataSource {
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return waypoint.count
}
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
let waypoints = waypoint[indexPath.row]
let cell =
tableView.dequeueReusableCell(withIdentifier: "Cell",
for: indexPath)
cell.accessoryType = .disclosureIndicator
cell.isUserInteractionEnabled = true
cell.textLabel?.text =
waypoints.value(forKeyPath: "name") as? String
return cell
}
func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) {
print("User selected cell....") //
}
Im have problem in the code (In using table view), declaration in function: didSelectRowAt - not called... What am I doing wrong? The declaration does not work.
Thanks very much for your help and all the answers.
解决方案
didSelectRowAt
is part of the UITableViewDelegate
protocol so your view controller needs to conform to it as well
extension ViewControllerWaypoints: UITableViewDataSource, UITableViewDelegate
and then you need to set the view controller to be the delegate of the table view in viewWillAppear
or similar
self.tableView.delegate = self
推荐阅读
- css - 如何使用 CSS 保持 16:9 和 4:3 之间的纵横比?
- vim - 如何通过 ctags 基于部分匹配在 vim 中自动完成
- angular5 - 取消选择效果 Radio Material2 Angular 5
- python - 硒无限期加载页面超时后如何获取新页面
- xslt - 使用 XSLT 1.0 从输入元素中增长输出元素
- sdn - Openflow Swicth 不推送 MPLS 标签。(开放日光)
- java - 如何使用 JSP 在电子邮件中插入链接
- docker - 奇怪的CouchDB错误,怎么理解?
- java - 使用 javaFX 根据操作系统计算窗口装饰的大小
- php - wp_mail 正在发送具有相同数据的消息