Swift 数组不返回所有值
Posted
技术标签:
【中文标题】Swift 数组不返回所有值【英文标题】:Swift Array Not Returning All Values 【发布时间】: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 代码使所有单元格标题等于数组中的最后一项。
【参考方案1】:
您不应该在cellForRowAt
中使用循环。获取特定于给定indexPath
的值:
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
【讨论】:
【参考方案2】:您不必遍历 cellForRowAt 中的每个元素,而是这样做:
cell.textLabel?.text = list[indexPath.row].categoryName
【讨论】:
感谢您的帮助【参考方案3】:您需要使用 indexPath 在索引处获取价值:
但是在category.categoryName
或你的结构中categoryName
的值没有定义。
我认为您将默认值分配给变量categoryName
,即categoryName = Mr.Lincoln
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
【讨论】:
这个答案与@rmaddy 的有什么不同! @triandicAnt 看看大胆的建议。 @triandicAnt 所有答案都在一两分钟内发布。没关系。以上是关于Swift 数组不返回所有值的主要内容,如果未能解决你的问题,请参考以下文章
检查 JSON 数组中是不是存在值,如果不存在则检查下一个数组(Swift / SwiftUI)