我是一名新手应用程序开发人员,无法在我的应用程序中显示多个部分和单元格。显示2个部分,第一个部分的3个单元格和第二个部分的2个单元格的最佳方法是什么?
仅处理numberOfRowsInSection-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0{
return 3
}else{
return 2
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
您将要做类似的事情。只要确保将YourCellIdentifier
和YourCellClass替换为您拥有的任何东西即可。
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 3
} else if section == 1 {
return 2
}
return 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(
withIdentifier: "YourCellIdentifier", for: indexPath) as! YourCellClass
// configure your cell here
return cell
}
}