View Controller未显示TableViewController中的数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了View Controller未显示TableViewController中的数据相关的知识,希望对你有一定的参考价值。

我创建了一个TableViewController,用于从Array中加载单元格的数据。 (除了加载的速度,还有任何提示吗?)

我的问题是我还创建了一个带有按钮的View Controller。我希望按钮根据在上一个视图中选择的单元格加载数据。

我在YouTube上观看了几个视频,这些视频都显示了相同的处理方法,但是当我尝试在我的代码中使用这个理论时,它不起作用。

这是原始的TableViewController代码

//  CatagoryTableViewController.swift

import UIKit

var list = [Categories]()

var myIndex = 0

class CatagoryTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        list.append(Categories(categoryText: "In The Mix", choiceA: "Ivy Smith", choiceB: "Shay West", choiceC: "Donald Allen", choiceD: "Zay (Wooo)"))
        list.append(Categories(categoryText: "Best Male Organization",choiceA: "People Standing United", choiceB: "Young Kings Movement", choiceC: "Gentlemen Qualities", choiceD: "" ))
        list.append(Categories(categoryText: "Best Female Organization", choiceA: "RSVP", choiceB: "STARS", choiceC: "NCNW", choiceD: "BSLS"))
        list.append(Categories(categoryText: "Best Dancer", choiceA: "Ansord Rashied", choiceB: "Donald Allen", choiceC: "Isis Ferguson", choiceD: "Jada Mosoey"))
        list.append(Categories(categoryText: "Most Likely To Brighten Your Day", choiceA: "Bar'rae Choice", choiceB: "Tytiana Jackson", choiceC: "Ivery Tanner", choiceD: "Chinonye Agu"))
        list.append(Categories(categoryText: "Best Modeling Troop", choiceA: "Ziana Fashion Club", choiceB: "We R 1 Family", choiceC: "", choiceD: ""))
        list.append(Categories(categoryText: "Best Male Friend Group", choiceA: "Quincy, Kraig, and Kiefer", choiceB: "WOOO", choiceC: "Kevin, Ivery, Kendell, and Marc", choiceD: "Dre, Eli, Jafari, and Ryan"))
        list.append(Categories(categoryText: "Best Female Friend Group", choiceA: "Omolara, Xela, Reggie, and Shania", choiceB: "Dior, Ashleigh, Tanya, Asha, Jazamine, and Aliea", choiceC: "Damo, Dani, Ty,Tati, and Ivy", choiceD: "Ahmani, Leshay, and Nyia"))
        list.append(Categories(categoryText: "Best Dressed Male", choiceA: "Dane Tyree", choiceB: "Ajamu Davis", choiceC: "Taj Green", choiceD: "Isiah Thomas"))
        list.append(Categories(categoryText: "Best Dressed Female", choiceA: "Imani Stamford", choiceB: "Ivy Smith", choiceC: "Tyler Murray", choiceD: "Kam"))
        list.append(Categories(categoryText: "Best DJ", choiceA: "Dj Topchoice", choiceB: "Dj Mizzy", choiceC: "Dj Che", choiceD: ""))
        list.append(Categories(categoryText: "Best Clothing Line", choiceA: "Visonary Society", choiceB: "Rare World", choiceC: "Handwritten", choiceD: "Soigne Y Pree"))
        list.append(Categories(categoryText: "Best Clothing Line", choiceA: "2016", choiceB: "2015", choiceC: "2014", choiceD: "2013"))
        list.append(Categories(categoryText: "Best Miss Lincoln", choiceA: "2016", choiceB: "2015", choiceC: "2014", choiceD: "2013"))
        list.append(Categories(categoryText: "Best Mr Lincoln", choiceA: "2016", choiceB: "2015", choiceC: "2014", choiceD: "2013"))
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      let count = list.count
            return count
        }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath)

     let category = list[indexPath.row]
      cell.textLabel?.text = category.categoryName
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        myIndex = indexPath.row
        performSegue(withIdentifier: "showNominees", sender: self)

    }
}

这里,如果View Controller应该显示基于所选单元格的数据。我没有错误,但是当程序正在运行时,按钮仍然是按钮。

//  ViewController.swift
  
import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var optionA: UIButton!
    @IBOutlet weak var optionB: UIButton!
    @IBOutlet weak var optionC: UIButton!
    @IBOutlet weak var optionD: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let nominees = list[myIndex]
        print(nominees)
        optionA.titleLabel?.text = nominees.optionA
        optionB.titleLabel?.text = nominees.optionB
        optionC.titleLabel?.text = nominees.optionC
        optionD.titleLabel?.text = nominees.optionD
    }
}
 */
答案

首先在tableViewController中声明你的列表变量。添加这个:

      self.performSegue(withIdentifier: "showNominees", sender: indexPath)  

在viewDidLoad方法之外创建提名变量,但在第二个viewController中创建。使用准备segue方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "showNominees" {
               if let vc = segue.destination as? ViewController {
                    guard let indexPath = sender as? IndexPath else { return }
                    vc.nominees = self.list[indexPath.row]
                  }            
            }
        }

人们已经提到你应该使用setTitle:

self.optionA.setTitle(nominees.choiceA, for: .normal)

它现在应该工作。希望如此。如果有帮助,请告诉我。

以上是关于View Controller未显示TableViewController中的数据的主要内容,如果未能解决你的问题,请参考以下文章

Activity View Controller 在新行中显示 URL

使用相同的 View Controller 进行添加、显示和编辑

Xcode Table View Controller with Nav Controller 不显示表格单元格,而是处理单元格中的数据

无法在 View Controller iOS Swift 中显示 TabBar

警告:尝试在已呈现的*上显示View Controller

使用jQuery AJAX请求CodeIgniter从Controller显示数据库中的数据到View而不刷新页面