【发布时间】:2018-03-08 04:59:19
【问题描述】:
我正在为 IOS 创建一个简单的投票应用程序。我创建了一个 tableviewcontroller,我想从数组中加载类别名称。
我的问题是数组只返回集合中的最后一个值(“林肯先生”)。
我要做的是将每个类别名称显示在一个单元格中,以便用户可以选择它,并且根据单击的单元格在下一个视图控制器上显示选项。
显示for in循环以返回单元格的代码部分是
for categoryText in list {
cell.textLabel?.text = categoryText.categoryName
}
//
// CatagoryTableViewController.swift
// xlsxreaderwriter
//
// Created by Ahmeeya Goldman on 2/11/18.
// Copyright © 2018 Ahmeeya Goldman. All rights reserved.
//
import UIKit
class CatagoryTableViewController: UITableViewController {
var list = [Categories]()
var myIndex = 0
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)
**for categoryText in list {
cell.textLabel?.text = categoryText.categoryName
}**
print(cell)
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "showNominees", sender: self)
}
}
【问题讨论】:
-
list.count返回什么? -
它返回 15 个单元格,因为该数组有 15 个类别。 list {} 中的 for categoryText 代码使所有单元格标题等于数组中的最后一项。
标签: arrays swift uitableview indexing