如何在Swift中向tableview添加具有不同类型原型单元的第二节?
我有一个在视图控制器中有两个原型单元的tableview。我希望每个单元格显示来自不同阵列的数据 下面是我的代码。我可以让tableview显示工作或学校,但不能同时显示两者。我不明白当每个单元格包含不同的数据源时,如何使表格显示cell1(作业)和cell2(学校) 答:我通过添加numberOfSections函数解决了这个问题(谢谢Robert)。如果其他人有此问题,请更新以下代码: 导入UIKit 类ViewController:UIViewController、UITableViewDelegate、UITableViewDataSource{如何在Swift中向tableview添加具有不同类型原型单元的第二节?,swift,uitableview,tableview,tableviewcell,Swift,Uitableview,Tableview,Tableviewcell,我有一个在视图控制器中有两个原型单元的tableview。我希望每个单元格显示来自不同阵列的数据 下面是我的代码。我可以让tableview显示工作或学校,但不能同时显示两者。我不明白当每个单元格包含不同的数据源时,如何使表格显示cell1(作业)和cell2(学校) 答:我通过添加numberOfSections函数解决了这个问题(谢谢Robert)。如果其他人有此问题,请更新以下代码: 导入UIKit 类ViewController:UIViewController、UITableView
let jobs = ["McDonalds", "Hardees", "Taco Bell"]
let schools = ["Univ of CO", "Univ of TX", "Univ of CA", "Univ of Camdenton"]
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == 0) {
return jobs.count
} else {
return schools.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "JobsCell", for: indexPath) as! Jobs
let job = jobs[indexPath.row]
cell.jobLbl.text = job
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "SchoolsCell", for: indexPath) as! Schools
let school = schools[indexPath.row]
cell.schoolLbl.text = school
return cell
}
}
}听起来您希望显示一个部分,其中单元格用于作业,然后显示第二个部分,单元格用于学校。如果是这种情况,则需要将节数设置为2,然后根据节数重写委托函数以适当响应 因此,
numberofrowsinssection
必须检查节号,然后返回特定节的行数。而cellForRowAt
将需要检查该部分,然后设置并返回给定行的作业或学校单元格
下面是您的示例中的情况:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let jobs = ["McDonalds", "Hardees", "Taco Bell"]
let schools = ["Univ of CO", "Univ of TX", "Univ of CA"]
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch(section) {
case 0: return jobs.count
default: return schools.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch(indexPath.section) {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "JobsCell", for: indexPath) as! Jobs
let job = jobs[indexPath.row]
cell.jobLbl.text = job
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "SchoolsCell", for: indexPath) as! Schools
let school = schools[indexPath.row]
cell.schoolLbl.text = school
return cell
}
}
}
这是模拟器的输出:
我编辑了您的标题,使其更具体地针对您的问题。如果你不同意,请随意回滚。谢谢罗伯特让我走上正轨。我修改了我的代码并添加了numberOfSections,它成功了!我将在我的原始帖子中分享我的更新代码。
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let jobs = ["McDonalds", "Hardees", "Taco Bell"]
let schools = ["Univ of CO", "Univ of TX", "Univ of CA"]
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch(section) {
case 0: return jobs.count
default: return schools.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch(indexPath.section) {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "JobsCell", for: indexPath) as! Jobs
let job = jobs[indexPath.row]
cell.jobLbl.text = job
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "SchoolsCell", for: indexPath) as! Schools
let school = schools[indexPath.row]
cell.schoolLbl.text = school
return cell
}
}
}