ios - UITableView 未显示所有部分

标签 ios swift uitableview tableview

我有一个问题,我想显示一个表格 View ,但按每个项目的“状态”分为几个部分。我知道如何使用简单的字符串数组来完成此操作,但我无法使用类(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
   }
}

在输出中,我只显示“第一部分”,并且没有名称,即使我设置了每个部分的名称和部分的数量。我到处都找过了,但找不到解决方案。

最佳答案

你的numberOfSectionstitleForHeader方法是错误的,应该是

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

    return 2
}

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

    return self.sections[section]
}

此外,您应该在硬编码的 numberOfSections 中返回 self.sections.count 而不是 return 2,以防您向其中添加另一个对象数组,您必须将 2 更改为数组现在拥有的任何元素。

关于ios - UITableView 未显示所有部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42907455/

相关文章:

ios - 强制 Wifi 弹出窗口 : Click a link to open Safari

xml - 在 Swift 中解析 XML 请求

ios - 如何从后台线程返回天气数据?

ios - 点击 UITextField 后跟 UIDatePicker 时,UITableView 向上滚动两次

ios - 自动调整包含 UICollectionView 的 UITableViewCell

ios - 如何将 UITableView 静态单元格设置为自定义高度?

ios - Firebase 异步函数,后台队列里有什么不在

ios - 具有嵌入式控件和自动中断文本的自动流动布局

objective-c - 为回调匹配 CoreData 保存通知

ios - "String?' 不可转换为 'NSString'”(Xcode 7 迁移后)