【问题标题】:Passing data from a static tableViewCell to another viewController?将数据从静态 tableViewCell 传递到另一个 viewController?
【发布时间】:2018-10-09 08:22:13
【问题描述】:

我有一个带有静态单元格的 tableView。

我不希望的是,当用户选择某个单元格时,该单元格中的文本会被传递给前一个 viewController。我以前从未使用过静态单元格,我似乎只能找到有关激活单元格的教程和其他问题,因此它们会导致另一个 viewController。

那么,当单元格被选中时,我如何传递数据(单元格中写入的内容)?

它是 didSelectRowAtIndexPath 中的代码吗?

我使用 segues 吗?那么必须有数百个segue,如果我有数百个单元格,用户可以选择,对吗?

谢谢!

【问题讨论】:

    标签: ios swift uitableview segue uistoryboardsegue


    【解决方案1】:

    首先,您将字符串数组分配给 DiaryQuestionLabel.text,它仅在destinationViewController 中接受字符串。只需在 destinationViewController 中将 selectedQuestion 的类型更改为 String 即可,如下所示。

    @IBOutlet weak var DiaryQuestionLabel: UILabel!
    var chosenQuestion: String = ""
    
    override func viewDidLoad() {
       super.viewDidLoad()
       DiaryQuestionLabel.text = chosenQuestion
    }
    

    在您的 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
        }
    }
    

    【讨论】:

    • 谢谢!看起来很合乎逻辑。只有我不知道该写什么,你在哪里写了“dataSourceArray”。我不确定我的 dataSourceArray 有名称吗?我使用以下代码来实现单元格中的值: override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "PotentielQuestion", for: indexPath)有名字还是我应该给它一个名字?
    • @AntonBjerg 您在单元格中显示什么?如果您只想在 DiaryQuestionLabel 中显示选定的索引,那么您只需要传递 vc?.chosenQuestion = String(selectedRowIndex.row)。
    • 我只是显示了几行纯字​​符串 - 这就是我需要传递给 DiaryQuestionLabel 的全部内容。但是当我在那里使用你的代码时,它给了我错误:无法使用类型为 '(Int?)' 的参数列表调用类型为 'String' 的初始化程序。也许它认为值是 Int 因为这些行,那就是:如果 indexPath.row == 0 { 等等......但是当我输入 Int(selectedRowIndex.row) 时,它给了我相反的错误...... .
    • 您是在 cellForRowAtIndexPath 的单元格中显示静态文本还是使用字符串数组在单元格中显示?
    • 静态文本!这只是一个“潜在问题”列表,当用户点击其中一个给定问题时,该问题应该转到另一个视图控制器中的标签。没有添加额外的单元格或任何东西。只是纯静态文本。如果描述我的问题含糊不清,我很抱歉!感谢您的帮助!
    【解决方案2】:

    您试图为变量分配错误的类型。这就是你的错误的原因。
    例如,
    您已将 chosenQuestion 定义为字符串值数组,即 [String],但您正尝试在以下语句 vc?.chosenQuestion = selectedRowIndex 中分配 IndexPath。

    为了解决您的问题,
    您需要利用已存储到selectedRowIndex 变量中的IndexPath 从数据源中提取特定字符串。

    例如, 如果您的数据源数组被调用,myArray,您可以执行以下操作:

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

    然后更改您的变量,
    var chosenQuestion = ""

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

    【讨论】:

    • 谢谢!这很有意义!只有我没有我的数据源数组的名称 - 我只是使用函数:let cell = tableView.dequeueReusableCell(withIdentifier: "PotentielQuestion", for: indexPath 所以我不知道如何引用这些数据源数组?
    • 不客气。我可以知道您是如何填充表格视图的吗?这将有助于澄清事情。
    • 覆盖 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "PotentielQuestion", for: indexPath) let label = cell.viewWithTag(第420章! UILabel if indexPath.row == 0 { label.text = "如果你有一个愿望,会是什么?"
    • 在注释字段中编写代码时看起来很乱......但我正在使用 dequeueReusableCell(withIdentifier:XXX) - 函数。然后手动在每个单元格中填写文本,说“如果 indexpath = 0, print:”bla bla bla”。有意义吗?
    【解决方案3】:

    文本标签是字符串类型。您正在为其分配一个字符串数组。 您在将 indexPath 分配给字符串数组时也犯了同样的错误。 你在搞乱类型。

    改成这样:

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

    将变量更改为

    var chosenQuestion = ""
    

    在 viewDidLoad() 中

    DiaryQuestionLabel.text = chosenQuestion
    

    【讨论】:

    • 我知道我在搞乱类型。我不知道,要使用哪种类型才能使其正常工作......看来我应该只使用这样的字符串:var selectedQuestion:String = "" & DiaryQuestionLabel.text = selectedQuestion,但我仍然得到错误:无法分配“IndexPath”类型的值?在 tableViewController 中键入“字符串”。这就是为什么我试图把它变成一个相信的数组,这可能会有所帮助吗?
    • 谢谢!只有这个我不明白,是你在tableViewController中分配了“yourLabel”吗?在那里我没有任何标签,除了贴在单元格上的标签,它没有名字。那么最后一段代码“yourLabel.text”是什么意思? :)
    • 在这种情况下,设为textLabel?.text
    【解决方案4】:

    一个简单的方法怎么样。这类似于Ali_Habeeb's answer,但有更多细节。

    在你的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)
    }
    

    在你的 newVC 中:

    class NewViewController: UIViewController {
    
      @IBOutlet weak var DiaryQuestionLabel: UILabel!
    
      var choosenQuestion = ""
    
      override func viewDidLoad() {
         super.viewDidLoad()
         DiaryQuestionLabel.text = choosenQuestion
      }
    }
    

    这是一种非常简单的格式,不会产生任何错误,如果有,请检查您的 dataArray。

    【讨论】:

    • 我认为这不起作用的原因是我没有在我的问题中提到,我只在 cellForRowAtIndexPath 的单元格中显示静态文本。所以就我而言,我没有任何所谓的“数据源数组”。
    • 您使用的是静态单元格吗?只有在这种情况下才可以使用静态文本。
    • 我现在。但是 tableView 在没有静态单元之前可以独立工作。但是现在我在尝试传递数据时仍然会遇到相同的错误。
    • 首先用完整的信息更新你的问题,通过你的这个问题表格,每个人都会把它当作动态单元格,并认为你正在使用 dataArray 或 dataModel 在其中显示文本。
    • 对于静态单元格,看看这里可能会得到任何帮助。 [stackoverflow.com/questions/31737114/…
    猜你喜欢
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    相关资源
    最近更新 更多