简体   繁体   English

将数据从静态 tableViewCell 传递到另一个 viewController?

[英]Passing data from a static tableViewCell to another viewController?

I have a tableView with static cells.我有一个带有静态单元格的 tableView。

What I wan't, is that when the user selects a certain cell, the text from this cell, is passed to the previous viewController.我不想要的是,当用户选择某个单元格时,该单元格中的文本将传递给前一个 viewController。 I have never worked with static cells before and I can only seem to find tutorials and other questions regarding activating the cells, so they leed to another viewController.我以前从未使用过静态单元格,我似乎只能找到有关激活单元格的教程和其他问题,因此它们导致了另一个视图控制器。

So how do I pass data (what's written in the cell), when the cell is selected?那么当单元格被选中时,如何传递数据(单元格中写入的内容)?

Is it a code in didSelectRowAtIndexPath?它是 didSelectRowAtIndexPath 中的代码吗?

Do I use segues?我使用 segue 吗? Then there will have to be hundreds of segues, if I have hundreds of cell, that the user can choose, right?然后必须有数百个转场,如果我有数百个单元格,用户可以选择,对吗?

Thanks!谢谢!

Text label is of type String.文本标签是字符串类型。 You are assigning an array of string to it.您正在为其分配一个字符串数组。 Also the same mistake you are doing with assigning indexPath to an array of Strings.将 indexPath 分配给字符串数组时也会犯同样的错误。 You are messing around with types.你在搞乱类型。

Change to this:改成这样:

vc?.chosenQuestion = tableView.cellForRow(at: selectedRowIndex).textLabel?.text

Change your variable to将您的变量更改为

var chosenQuestion = ""

And in viewDidLoad()而在 viewDidLoad()

DiaryQuestionLabel.text = chosenQuestion

You are attempting to assign the wrong type to your variables.您正试图为变量分配错误的类型。 That is the cause of your errors.这就是你的错误的原因。
For example,例如,
You have defined chosenQuestion as an array of String values, ie [String] , but you are attempting to assign an IndexPath at the following statement, vc?.chosenQuestion = selectedRowIndex .您已将chosenQuestion定义为字符串值数组,即[String] ,但您试图在以下语句vc?.chosenQuestion = selectedRowIndex分配 IndexPath 。

To fix your problem,为了解决您的问题,
You need to extract the specific string from your data source by utilising the IndexPath that you have stored into the selectedRowIndex variable.您需要利用存储在selectedRowIndex变量中的IndexPath从数据源中提取特定字符串。

For example, If your data source array is called, myArray , you can do the following:例如,如果您的数据源数组被调用, myArray ,您可以执行以下操作:

var selectedRowIndex = self.tableView.indexPathForSelectedRow
vc?.chosenQuestion = myArray[selectedRowIndex]

and then change your variable,然后改变你的变量,
var chosenQuestion = ""

Finally, inside viewDidLoad() :最后,在viewDidLoad()
DiaryQuestionLabel.text = chosenQuestion

First you are assigning Array of String to DiaryQuestionLabel.text which accepts string only in destinationViewController.首先,您将字符串数组分配给 DiaryQuestionLabel.text,它仅在 destinationViewController 中接受字符串。 Just change the type of chosenQuestion to String in destinationViewController as below.只需在 destinationViewController 中将 selectedQuestion 的类型更改为 String ,如下所示。

@IBOutlet weak var DiaryQuestionLabel: UILabel!
var chosenQuestion: String = ""

override func viewDidLoad() {
   super.viewDidLoad()
   DiaryQuestionLabel.text = chosenQuestion
}

In your tableViewController You need to pass the value of selected index from your datasource array which you have used to set the data in your tableviewcell.在您的 tableViewController 中,您需要从数据源数组中传递所选索引的值,该值用于在 tableviewcell 中设置数据。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     if segue.destination is UpdatingIdentifiers {
     let vc = segue.destination as? UpdatingIdentifiers
     let selectedRowIndex = self.tableView.indexPathForSelectedRow()
     // Get tableviewcell object from indexpath and assign it's value to chosenQuestion of another controller.  
     let cell = yourtableview.cellForRow(at: selectedRowIndex)
     let label = cell.viewWithTag(420) as! UILabel
     vc?.chosenQuestion = label.text
    }
}

How about a simple way.一个简单的方法怎么样。 This is similar to Ali_Habeeb's answer with more details.这与Ali_Habeeb 的回答类似,有更多细节。

In your didSelectRowAt :在你didSelectRowAt

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let question = yourDataArray[indexPath.row]
    let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
    let newVC = storyboard.instantiateViewController(withIdentifier: "newVCIdentifier") as! NewViewController
    newVC.choosenQuestion = question
    self.show(newVC, sender: self)
}

In your newVC:在你的新VC中:

class NewViewController: UIViewController {

  @IBOutlet weak var DiaryQuestionLabel: UILabel!

  var choosenQuestion = ""

  override func viewDidLoad() {
     super.viewDidLoad()
     DiaryQuestionLabel.text = choosenQuestion
  }
}

This is a very simple format, and should not produce any error and if does, then check your dataArray.这是一种非常简单的格式,不应产生任何错误,如果有,请检查您的 dataArray。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM