Ios UITableView未显示所有部分
我有一个问题,我想显示一个tableview,但每个部分都由每个项目的“状态”分隔。我知道如何使用简单的字符串数组,但我无法使用类(Aluno)数组实现这一点,以下是我目前的代码:Ios UITableView未显示所有部分,ios,swift,uitableview,tableview,Ios,Swift,Uitableview,Tableview,我有一个问题,我想显示一个tableview,但每个部分都由每个项目的“状态”分隔。我知道如何使用简单的字符串数组,但我无法使用类(Aluno)数组实现这一点,以下是我目前的代码: import UIKit class DeliveriesTVC: UITableViewController { let sections = ["Delivered", "Not Delivered"] var studentList: [Array<Student>] = [[]
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
}
}
导入UIKit
类DeliveriesTVC:UITableViewController{
let sections=[“已交付”、“未交付”]
var studentList:[数组]=[],[]
重写func viewDidLoad(){
super.viewDidLoad()
因为我在0…5{
学生列表[0]。追加(学生(Alunome:“Aluno\(i)”,Alunomg:“dani_测试”,Alunstatus:“已交付”))
}
因为我在6…10{
学生名单[1]。追加(学生(校名:“校名(i)”,校名:“dani_测试”,校名状态:“未交付”))
}
self.title=“交付”
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
如果让cellEntrega=tableView.dequeueReusableCell(标识符为:“EntregaCell”,for:indexath)作为?EntregaCell{
让entregaCell=studentList[indexPath.section][indexPath.row]
//在这里,我在TableViewCell类中调用一个函数来更新单元格本身
cellEntrega.UpdateLunoUI(Aluno:entregaCell)
返回Cellenterga
}否则{
返回UITableViewCell()
}
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回listaAlunos[section]。计数
}
func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String{
返回自我部分[部分]
}
func numberOfSectionsInTableView(tableView:UITableView)->Int{
返回2
}
}
在输出中,我只显示“first section”(第一节),没有名称,即使我设置了每个节的名称和节数。我到处找了,但找不到解决办法
numberOfSectionInTableView
函数,它不应该是覆盖func numberOfSectionsInTableView(在tableView:UITableView中)->Int吗{
返回2
}
在
表格视图前面的
中有一个:UITableView
UITableView
的委托
和数据源
连接到何处numberOfSectionInTableView
函数,它不应该是覆盖func numberOfSectionsInTableView(在tableView:UITableView中)->Int吗{
返回2
}
在
表格视图前面的
中有一个:UITableView
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更改为数组现在拥有的任何元素。您的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更改为数组现在拥有的任何元素。您不需要在UITableViewController
中设置委托和数据源,您是正确的。不知何故,我认为它在UIViewController
中。您不需要在UITableViewController
中设置委托和数据源,您是正确的。不知怎的,我认为它在UIViewController
中。你真的需要弄清楚你的数据源。您的cellForRowAt
基于名为studentList
的数组。您的numberOfRowsInSection
基于名为listaAlunos
的数组(可能是翻译疏忽)。您的标题forheaderinsection
基于另一个名为sections
的数组。并且您不必要地使用值2
对numberOfSections进行硬编码。别硬拼那个号码。是的,这是个翻译问题,他们实际上是一样的,对不起!我还硬编码了部分的数量,因为我已经尝试了所有的方法,最后我的问题是函数错误,我不知道。谢谢你的帮助!你真的需要弄清楚你的数据源。您的cellForRowAt
基于名为studentList
的数组。您的numberOfRowsInSection
基于名为listaAlunos
的数组(可能是翻译疏忽)。您的标题forheaderinsection
基于另一个名为sections
的数组。并且您不必要地使用值2
对numberOfSections进行硬编码。别硬拼那个号码。是的,这是个翻译问题,他们实际上是一样的,对不起!我还硬编码了部分的数量,因为我已经尝试了所有的方法,最后我的问题是函数错误,我不知道。谢谢你的帮助!