[英]UITableView not showing all sections
我有一個問題,我想顯示一個表格視圖,但每個項目的“狀態”按節分開。 我知道如何使用簡單的字符串數組來執行此操作,但是我無法使用類(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
}
}
在輸出中,即使設置了每個節的名稱和節數,我也只顯示了“第一節”,而沒有名稱。 我到處都看過,但找不到解決方案。
對於您的numberOfSectionInTableView
函數,應該不是
override func numberOfSectionsInTableView(in tableView: UITableView) -> Int { return 2 }
用in
在前面tableView: UITableView
?
我看不到將UITableView
的delegate
和datasource
連接到哪里。
這是一個教程,向您展示有關使用UITableView的信息。
看一下“基本表視圖”-“步驟3:設置數據源和委托”
您的numberOfSections
和titleForHeader
方法錯誤,應該是
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
而不是在numberOfSections
中return 2
,因為要為該數組添加另一個對象,您必須將2更改為該數組現在具有的任何元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.