arrays - Swift 数组不返回所有值

标签 arrays swift uitableview indexing

我正在为 IOS 创建一个简单的投票应用程序。我创建了一个 tableviewcontroller,我想从数组中加载类别名称。

我的问题是数组仅返回集合中的最后一个值(“Mr.Lincoln)。

我想要做的是将每个类别名称显示在单元格中,以便用户可以选择它,并根据单击的单元格在下一个 View Controller 上显示选项。

显示 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)
    }
}

最佳答案

您不应在 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
}

关于arrays - Swift 数组不返回所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49165722/

相关文章:

javascript - 使用 angularjs 的 Google 通讯录 API

swift - 如何使用mlmodel对UIImage进行预测?

ios - 是否可以使用自动布局获取动态表格 View 部分标题高度?

ios - 目标 - C - UITableView cellForRowAtIndexPath 从未调用过

swift - 在 Swift 中同步异步内容

ios - UIImage 未显示在 UITableView 中

php - 如何在循环中中断索引数组并在php中使用

javascript - Reduce() 一个已经被减少的元素的对象

arrays - 如何在Kotlin中将多维数组转换为多维列表?

ios - iOS 导航 Controller 代码的正确位置?