swift - 单击单元格后,它向我传递了 2 个 View ,我该如何修复它?

标签 swift xcode uinavigationcontroller uitabbarcontroller

我有一个应用程序,它以带有 3 个项目的选项卡栏开头,其中 2 个只是常规 View Controller ,其中一个是导航 Controller ,后面是表格 View ,当我单击一个单元格时,我想传递 2字符串到标签并传递到新的 View Controller 以显示标签,我的问题是,在我单击单元格后,它看起来像跳转 2 View Controller ,直到我想要的 View Controller ,而第一次我没有看不到我传递的数据,只有在我按下返回 TableView Controller 后(这里我需要从2个 View Controller 返回给他),请帮助我修复我的代码,我想传递数据和移动到新的 View Controller ,当我想返回 TableView 时,我需要单击返回

//表格 View Controller ,在他之前我有一个导航 Controller

//segue的标识符是“Details”

import UIKit

class HistoryTableViewController: 
UIViewController,UITableViewDelegate,UITableViewDataSource {


@IBOutlet weak var historyTableView: UITableView!
var historyArray = [history]()
var foods = ["Milk","Honey","Salt","Bread","Banana"]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return foods.count
    //historyArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = historyTableView.dequeueReusableCell(withIdentifier: "cell") as? HistoryCellViewController
        else {return UITableViewCell()}

    //  cell.ageLabel.text = String(format:"%f", historyArray[indexPath.row].age)
    //  cell.genderLabel.text = historyArray[indexPath.row].gender
    // cell.ageLabel.text = String(format:"%f", 23)
    cell.genderLabel.text = foods[indexPath.row]
    cell.ageLabel.text = foods[indexPath.row]
    // cell.genderLabel.text = "Bar"
    //cell.historyImage.image = nil
    return cell

}

var selectedAge:String = " "
var selectedGender:String = " "
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    selectedAge = self.foods[indexPath.row]
    selectedGender = self.foods[indexPath.row]
    performSegue(withIdentifier: "Details", sender: self)


}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "Details"{
        let vc = segue.destination as! HistoryDetailsViewController
        vc.age = selectedAge
        vc.gender = selectedGender
    }
}


func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCell.EditingStyle.delete {

        foods.remove(at: indexPath.row)
        historyTableView.reloadData()
    }

}
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

}

//点击单元格后我想要传递的 View Controller

 import UIKit

 class HistoryDetailsViewController: UIViewController {
 @IBOutlet weak var ageLabel: UILabel!
 @IBOutlet weak var genderLabel: UILabel!
 var age:String?
 var gender:String?
 override func viewDidLoad() {
    super.viewDidLoad()
      ageLabel.text = self.age
      genderLabel.text = self.gender
    // Do any additional setup after loading the view.
}

 //
 //    @IBAction func Back(_ sender: UIBarButtonItem) {
 //        
//      //  self.tabBarController?.selectedIndex = 0
//    }



}

最佳答案

首先,在 tableView(_:cellForRowAt:)你是typecasting dequeued cellHistoryCellViewController .

我的问题是 - type 是什么?的HistoryCellViewController ?是 UITableViewCell或者是其他东西?如果是UITableViewCell您必须将其重命名为更相关的名称,例如 HistoryCell 。有点困惑。

方法 1:

现在来谈谈您面临的实际问题。而不是使用 segue ,尝试按HistoryDetailsViewController像这样手动,

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let controller = self.storyboard?.instantiateViewController(withIdentifier: "HistoryDetailsViewController") as? HistoryDetailsViewController {
        controller.age = self.foods[indexPath.row]
        controller.gender = self.foods[indexPath.row]
        self.navigationController?.pushViewController(controller, animated: true)
    }
}

不需要额外的变量 - selectedAgeselectedGender .

方法 2:

如果您想使用 segue 来执行此操作本身,然后不要调用 performSegue(withIdentifier:sender:)tableView(_:didSelectRowAt:) ,即删除以下代码行,

performSegue(withIdentifier: "Details", sender: self)

这是因为 "Details" segue正在执行两次 - 一次通过 storyboard另一个通过上面的代码行。

关于swift - 单击单元格后,它向我传递了 2 个 View ,我该如何修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56469359/

相关文章:

ios - 在父 TableView 的同级详细 View 之间导航

iOS::导航栏上的按钮仅出现在 Root View Controller 上

ios - Swift - 如何从 UITableViewCell 中的按钮调用 UIVIewController

ios - 检索缩放后的 UIView 的正确位置

objective-c - 确保我没有在 OSX 上使用任何不可用的 API 的最佳方法是什么?

ios - 我应该如何删除错误的提交并移动原点

ios - 应用程序在后台时未收到 Swift FCM 数据消息 (iOS 10)

swift - 在 init 上将异步数据加载到数据模型中

objective-c - 无法从 Objective-C 调用 Swift 函数

ios - 注销后更改rootviewcontroller