视图控制器不显示来自 TableViewController 的数据
View Controller not showing Data from TableViewController
我创建了一个 TableViewController,它从数组中加载单元格的数据。 (除加载速度外工作正常,有什么提示吗?)
我的问题是我还创建了一个连接有按钮的视图控制器。我希望按钮根据上一个视图中选择的单元格加载数据。
我在 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)
}
}
此处,如果视图控制器应显示基于所选单元格的数据。我没有错误,但当程序为 运行.
时按钮仍然显示按钮
// 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 方法内创建提名变量。
使用 prepare for 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)
它应该可以工作 now.Hopefully。
如果有帮助,请告诉我。
我创建了一个 TableViewController,它从数组中加载单元格的数据。 (除加载速度外工作正常,有什么提示吗?)
我的问题是我还创建了一个连接有按钮的视图控制器。我希望按钮根据上一个视图中选择的单元格加载数据。
我在 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)
}
}
此处,如果视图控制器应显示基于所选单元格的数据。我没有错误,但当程序为 运行.
时按钮仍然显示按钮// 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 方法内创建提名变量。 使用 prepare for 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)
它应该可以工作 now.Hopefully。 如果有帮助,请告诉我。