Segue没有从自定义tableView单元触发

时间:2018-08-11 16:11:20

标签: ios swift uitableview tableview segue

我遇到的是我确定是初学者的错误。我有一个TableViewController,应该在选择自定义单元格后选择第二个TableViewController。

我过去曾从自定义单元中锁定过一个UIViewController,但我一生都无法弄清楚为什么这种锁定不会触发。这是应该触发segue的整个控制器的代码。预先感谢!

import UIKit

class SourcesViewController: UITableViewController {

var sources: [Source] = []

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.isNavigationBarHidden = false
    self.navigationItem .setHidesBackButton(true, animated: false)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}




// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sources.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell_01", for: indexPath) as? SourceCell

        else {
            return tableView.dequeueReusableCell(withIdentifier: "cell_01", for: indexPath)
    }

    let currentSource = sources[indexPath.row]

    cell.sourceLabel.text = currentSource.name

    return cell
}




// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let indexPath = tableView.indexPathForSelectedRow {
        let sourceToSend = sources[indexPath.row]
        if let destination = segue.destination as? ArticlesViewController {
            destination.source = sourceToSend
        }
    }
  }

}

1 个答案:

答案 0 :(得分:-1)

您的segue不会被调用,因为您没有调用它。 UIViewController具有perfromSegue方法,您应该为此使用该方法。

据我所见,您希望在点击单元格后执行segue。要实现此目的,请添加UITableViewDelegate方法并从那里执行segue:

/// - SeeAlso: UITableViewDelegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "your_identifier_from_the_storyboard", sender: nil)
}