我创建了一个TableViewController,用于从Array中加载单元格的数据。 (除了加载的速度,还有任何提示吗?)
我的问题是我还创建了一个带有按钮的视图控制器。我希望按钮根据在上一个视图中选择的单元格加载数据。
我在YouTube上观看了一些视频,这些视频都显示了相同的处理方法,但是当我尝试在我的代码中使用此理论时它无法正常工作。
这是原始的TableViewController代码
var i;
for(i = 0; i < myArr.length; i++) {
if((myArr[i].name === added.name) && (myArr[i].age === added.age) && (myArr[i].info === added.info) {
}
else {
myArr.push(added);
}
}
这里,如果View Controller应该显示基于所选单元格的数据。我没有错误,但是当程序正在运行时,按钮仍然是按钮。
// 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)
}
}
&#13;
答案 0 :(得分:0)
首先在tableViewController中声明你的列表变量。加上这个:
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]
}
}
}
在viewDidLoad方法之外创建提名变量,但在第二个viewController中创建。 使用准备segue方法:
self.optionA.setTitle(nominees.choiceA, for: .normal)
人们已经提到过你应该使用setTitle:
auto
现在应该可以了。希望如此。 如果有帮助,请告诉我。