如何在 SWIFT 3 中对 UITableView 执行部分和单元格的动态创建

Rev*_*goL 2 dynamic uitableview ios swift swift3

我有一个示例代码,但不会显示每节的行,因为numberOfRowsInSection不再存在SWIFT 3

我目前有 ff 代码:

let section = ["pizza", "deep dish pizza", "calzone"]

let items = [["Margarita", "BBQ Chicken", "Pepperoni"], ["sausage", "meat lovers", "veggie lovers"], ["sausage", "chicken pesto", "prawns", "mushrooms"]]

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {

    return self.section[section]

}

override func numberOfSections(in tableView: UITableView) -> Int {
    return self.section.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)

    // Configure the cell...

    cell.textLabel?.text = self.items[indexPath.section][indexPath.row]

    return cell
}
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明

有人可以显示更新的正确代码swift 3吗?谢谢!

Kar*_*raj 6

有关您的信息numberOfRowsInsection存在于 中swift3,请参见下文

 override func numberOfSections(in tableView: UITableView) -> Int {
    return section.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items[section].count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
    cell?.textLabel?.text = items[indexPath.section][indexPath.row]
    return cell!
}
Run Code Online (Sandbox Code Playgroud)

谢谢:)