Swift - didSelectRowAt indexPath next VC 没有出现/没有故事板/

Swift - didSelectRowAt indexPath next VC not showing up /no storyboards/

我对 "didSelectRowAt indexPath" 有疑问,因为行没有触发下一个 VC。 在控制台中,我为特定行打印了字符串,因此选择一行可以正常工作,但是下一个 VC 没有打开。 我当然试图找到有关堆栈溢出和 google 的解决方案,但是我找不到解决方案。 下面我粘贴: 主要VC -> ViewController 和 下一个 VC -> DetailViewCONtroller

预先感谢您的帮助。

主要VC:

import UIKit

class ViewController: UIViewController {

  let cellID = "Picture"
  let tableViewCell = TableViewCell()

  let tableWithImages: UITableView = {
    let table = UITableView()
    table.rowHeight = 50
    table.translatesAutoresizingMaskIntoConstraints = false
    return table
  }()

  var pictures: [String] = [String]()

  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    tableWithImages.delegate = self
    tableWithImages.dataSource = self
    tableWithImages.register(TableViewCell.self, forCellReuseIdentifier: cellID)
    fileManagerSetup()
    setupView()
  }

  func fileManagerSetup(){
   let fm = FileManager.default
    let path = Bundle.main.resourcePath!
    let items = try! fm.contentsOfDirectory(atPath: path)
    for item in items {
      if item.hasPrefix("nssl"){
        pictures.append(item)
      }
    }
    print(pictures)
  }

  private func setupView(){
    view.addSubview(tableWithImages)
    tableWithImages.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
    tableWithImages.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
    tableWithImages.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
    tableWithImages.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return pictures.count
  }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
    cell.textLabel?.text = pictures[indexPath.row]
    return cell
  }

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let detailVC = DetailViewController()
    detailVC.selectedImage = pictures[indexPath.row]
      self.navigationController?.pushViewController(detailVC, animated: true)
    print("Row tapped: \(pictures[indexPath.row])")
  }
}

第二个VC:

import UIKit

class DetailViewController: UIViewController {

  var imageView: UIImageView = {
    var picture = UIImageView()
    picture.translatesAutoresizingMaskIntoConstraints = false
    return picture
  }()

  var selectedImage: String?

    override func viewDidLoad() {
        super.viewDidLoad()
      view.backgroundColor = .white
      setupView()
      if let imageToLoad = selectedImage {
        imageView.image = UIImage(named: imageToLoad)
      }
    }

  private func setupView(){
    view.addSubview(imageView)
    imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
    imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
    imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
    imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  }

}

我假设第一个视图控制器不在导航堆栈中。添加断点并确定当您尝试将新视图控制器推送到导航堆栈时 navigationController 是否不为 nil。