如何将具有不同类型原型单元格的第二部分添加到 Swift 中的表格视图?

How do you add a second section with a different type of prototype cell to a tableview in Swift?

我有一个 table视图,视图控制器中有 2 个原型单元格。我希望每个单元格显示来自不同数组的数据。

下面是我的代码。我可以获得 table 视图来显示工作或学校,但不能同时显示两者。我不明白当每个单元格包含不同的数据源时,如何让 table 显示 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

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

因此 numberOfRowsInSection 将必须检查节号,然后 return 该特定节的行数。 cellForRowAt 将需要检查该部分,然后设置并 return 给定行的工作或学校单元。

这是您的示例中的样子:

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
        }
    }

}

这是模拟器中的输出: