Swift数组不返回所有值

时间:2018-03-08 04:59:20

标签: arrays swift uitableview indexing

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

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

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

显示for循环以返回单元格的代码部分是

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

3 个答案:

答案 0 :(得分:0)

您不必迭代 cellForRowAt 中的每个元素而是执行此操作:

cell.textLabel?.text = list[indexPath.row].categoryName

答案 1 :(得分:0)

您不应该在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 :(得分:0)

您需要使用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
}