【发布时间】:2021-03-17 03:43:05
【问题描述】:
我正在尝试在 UITableview 部分添加两个自定义 UITableViewcell,但第一部分显示正确,而第二部分自定义单元格未显示。我该如何解决这个问题?
这是我的代码。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return itemsTitleArray.count
} else {
return approvalsArray.count
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return sectionArray.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionArray[section]
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
//first cell
let cell = tableView.dequeueReusableCell(withIdentifier:“FirstTableViewCell", for: indexPath) as! FirstTableViewCell
cell.itemTitleLabel?.text = itemsTitleArray[indexPath.row]
cell.backgroundColor = .red
return cell
case 1:
//second cell
let cell = tableView.dequeueReusableCell(withIdentifier:“SecondTableViewCell", for: indexPath) as! SecondTableViewCell
cell.approvalStausLabel.text = approvalsArray[indexPath.row]
cell.backgroundColor = .yellow
return cell
default:
return UITableViewCell()
}
}
【问题讨论】:
-
默认情况下返回
Custom TableViewCell而不是return UITableViewCell() -
我也试过了,但没用。 @AmirKhan Khan
-
我试试你的代码,它工作正常,也许你的
tableView高度不是动态的,tableview 只显示第一部分的内容? -
你有第二节标题吗?