gpt4 book ai didi

swift - 如何在 Swift 中将具有不同类型原型(prototype)单元格的第二部分添加到 TableView 中?

转载 作者:行者123 更新时间:2023-11-28 06:19:28 24 4
gpt4 key购买 nike

我在 View Controller 中有一个带有 2 个原型(prototype)单元格的 TableView 。我希望每个单元格显示来自不同数组的数据。

下面是我的代码。我可以让表格 View 显示工作或学校,但不能同时显示两者。当每个单元格包含不同的数据源时,我不明白如何让表格显示 cell1(工作)然后显示 cell2(学校)。

screenshot

Import UIKit

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 tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return jobs.count

}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "JobsCell", for: indexPath) as! Jobs

let job = jobs[indexPath.row]
cell.jobLbl.text = job

return cell


}

}

回答:我通过添加 numberOfSections 函数解决了这个问题(感谢 Robert)。如果其他人有此问题,请更新下面的代码:

导入 UIKit

ViewController 类:UIViewController、UITableViewDelegate、UITableViewDataSource {

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,然后重写您的委托(delegate)函数以根据节数做出适当响应。

因此 numberOfRowsInSection 将必须检查节号,然后返回该特定节的行数。 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
}
}

}

这是模拟器中的输出:

Simulator output image

关于swift - 如何在 Swift 中将具有不同类型原型(prototype)单元格的第二部分添加到 TableView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44098980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com