【问题标题】:How do you add a second section with a different type of prototype cell to a tableview in Swift?如何在 Swift 中的 tableview 中添加具有不同类型原型单元格的第二部分?
【发布时间】:2017-10-21 07:00:06
【问题描述】:

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

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

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

    }

}

}

【问题讨论】:

  • 我编辑了您的标题,使其更具体地解决您的问题。如果您不同意,请随时回滚。

标签: swift uitableview tableview tableviewcell


【解决方案1】:

听起来您想显示一个部分,其中包含工作单元格,然后是第二个部分,其中包含学校单元格。如果是这种情况,您需要将节数设置为 2,然后重写您的委托函数以根据节号做出适当的响应。

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

}

这是模拟器中的输出:

【讨论】:

  • 感谢罗伯特让我走上正轨。我修改了我的代码并添加了 numberOfSections 并且它起作用了!我将在我的原始帖子中分享我更新的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
相关资源
最近更新 更多