UITableView没有显示所有部分

时间:2017-03-20 15:21:58

标签: ios swift uitableview tableview

我有一个问题,我想要显示一个tableview,但是按照" status"分开。每个项目。我知道如何使用一个简单的字符串数组来完成它,但我无法使用类(Aluno)数组来完成这项工作,到目前为止我的代码是这样的:

import UIKit

class DeliveriesTVC: UITableViewController {

    let sections = ["Delivered", "Not Delivered"]
    var studentList: [Array<Student>] = [[], []]


    override func viewDidLoad() {
        super.viewDidLoad()

        for i in 0...5{
            studentList[0].append(Student(alunoNome: "Aluno \(i)", alunoImg: "dani_test", alunoStatus: "Delivered"))
        }

        for i in 6...10{
            studentList[1].append(Student(alunoNome: "Aluno \(i)", alunoImg: "dani_test", alunoStatus: "Not Delivered"))

        }

        self.title = "Deliveries"
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if let cellEntrega = tableView.dequeueReusableCell(withIdentifier: "EntregaCell", for: indexPath) as? EntregaCell {

            let entregaCell = studentList[indexPath.section][indexPath.row]

            // here i call a function in my TableViewCell class that update the cell itself
            cellEntrega.updateAlunoUI(Aluno: entregaCell)

            return cellEntrega

        } else {
            return UITableViewCell()
        }
    }


 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return listaAlunos[section].count
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return self.sections[section]
}

   func numberOfSectionsInTableView(tableView: UITableView) -> Int {
       return 2
   }
}

在输出中,我只是得到了第一部分&#34;显示,没有名称,甚至我设置每个部分的名称和部分的数量。我到处都看,但我找不到解决方案。

2 个答案:

答案 0 :(得分:0)

  1. 对于numberOfSectionInTableView功能,不应该是override func numberOfSectionsInTableView(in tableView: UITableView) -> Int { return 2 }in前面有tableView: UITableView的情况吗?

  2. 我看不到您与UITableView的{​​{1}}和delegate的关联位置。

  3. Here是一个向您展示使用UITableView的教程。

    查看“基本表视图” - “步骤3:设置数据源和委托”

答案 1 :(得分:0)

您的numberOfSectionstitleForHeader方法错误,应为

override func numberOfSections(in tableView: UITableView) -> Int {

    return 2
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    return self.sections[section]
}

此外,您应该在self.sections.count进行硬编码时返回return 2而不是numberOfSections,因为如果您向阵列添加另一个对象,则必须将2更改为数组现在。