首页 > 解决方案 > 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.

标签: swiftxcode

解决方案


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

推荐阅读