我有一个问题,我想显示一个表格 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
}
}
在输出中,我只显示“第一部分”,并且没有名称,即使我设置了每个部分的名称和部分的数量。我到处都找过了,但找不到解决方案。
最佳答案
你的numberOfSections
和titleForHeader
方法是错误的,应该是
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/