UITableView 未显示自定义单元格的第二个实例



我正在使用动态UITableView,自定义UITableViewCells和内置的Right Detail单元格创建一个表单。我的表格视图有 3 个部分。表视图不会呈现我的任何自定义单元格的第二次出现,但会呈现第一次出现。我的自定义单元格的标识符是 textFieldCell 和 switchCell。在第 0 行 0 处使用自定义单元格效果很好,但对第 1 节第 0 行单元格的下一个引用只显示一个空单元格。第 2 部分第 0 行的自定义单元格也只显示一次。对此自定义单元格的另一个引用也是空的。内置单元格标题字段显示单元格每次调用时都会呈现良好。

有谁知道表视图不呈现对自定义单元格的第二个引用的原因?我正在使用的cellForRowAt函数粘贴在下面。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Setup the first section
if indexPath.section == 0 {
if indexPath.row == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as? TextFieldCell else {return UITableViewCell()}
cell.configureCell(withPlaceHolder: "Title")
return cell
} else {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "fieldDisplayCell", for: indexPath) as? FieldDisplayCell else {return UITableViewCell()}
cell.configureCell(withFieldLabel: "Type", andValueLabel: "Date")
return cell
}
}
//Setup the second section
if indexPath.section == 1 {
if indexPath.row == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as? TextFieldCell else {return UITableViewCell()}
cell.configureCell(withPlaceHolder: "Location")
} else if indexPath.row == 1 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "fieldDisplayCell", for: indexPath) as? FieldDisplayCell else {return UITableViewCell()}
cell.configureCell(withFieldLabel: "Start", andValueLabel: "June 17, 2018")
return cell
} else {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "fieldDisplayCell", for: indexPath) as? FieldDisplayCell else {return UITableViewCell()}
cell.configureCell(withFieldLabel: "End", andValueLabel: "June 17, 2018")
return cell
}
}
//Setup the third section
if indexPath.section == 2 {
if indexPath.row == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "switchCell", for: indexPath) as? SwitchCell else {return UITableViewCell()}
cell.configureCell(withFieldLabal: "Remind")
return cell
} else {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "switchCell", for: indexPath) as? SwitchCell else {return UITableViewCell()}
cell.configureCell(withFieldLabal: "Track")
}
}
return SwitchCell()
}

似乎您没有返回第 1 部分第 0 行和第 2 部分第 1 行的单元格。

因此,您必须在这些行中添加return cell

在第 1 节第 0 行:

if indexPath.row == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as? TextFieldCell else {return UITableViewCell()}
cell.configureCell(withPlaceHolder: "Location")
return cell
}

在第 2 节第 1 行:

else {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "switchCell", for: indexPath) as? SwitchCell else {return UITableViewCell()}
cell.configureCell(withFieldLabal: "Track")
return cell
}

您是否覆盖了 numberOfRowsInSection 和 numberOfSections 函数? 没有它,tableView 将不知道要呈现多少行或多少节:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch (section)
{
case 0: return 2
case 1: return 2
case 2: return 2
default: return 2
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}

最新更新