Swift-didSelectRowAt indexPath下一个VC不显示/ no故事板/

问题描述 投票:0回答:1

我对“ didSelectRowAt indexPath”有问题,因为行没有触发下一个VC。在控制台中,我已经为特定行打印了字符串,因此选择一行就可以了,但是下一个VC无法打开。我当然试图在堆栈溢出和谷歌上找到解决方案,但是我找不到解决方案。下面我粘贴:MainVC-> 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
  }

}
ios swift5 programmatically-created
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.