Ios UITableView不显示自定义单元格的第二个实例
我正在使用动态UITableView、自定义UITableViewCells和内置的右详细信息单元格创建表单。我的tableview有3个部分。tableview不会渲染任何自定义单元格的第二个匹配项,但会渲染第一个匹配项。自定义单元格的标识符是textFieldCell和switchCell。在第0节第0行使用自定义单元格会呈现良好效果,但对第1节第0行单元格的下一次引用仅显示空单元格。第2节第0行的自定义单元格也只显示一次。对该自定义单元格的其他引用也为空。内置单元格标题fieldDisplayCell在每次调用时都会呈现良好的效果 有人知道tableview不呈现对自定义单元格的第二个引用的原因吗?我正在使用的cellForRowAt函数粘贴在下面Ios UITableView不显示自定义单元格的第二个实例,ios,swift,uitableview,Ios,Swift,Uitableview,我正在使用动态UITableView、自定义UITableViewCells和内置的右详细信息单元格创建表单。我的tableview有3个部分。tableview不会渲染任何自定义单元格的第二个匹配项,但会渲染第一个匹配项。自定义单元格的标识符是textFieldCell和switchCell。在第0节第0行使用自定义单元格会呈现良好效果,但对第1节第0行单元格的下一次引用仅显示空单元格。第2节第0行的自定义单元格也只显示一次。对该自定义单元格的其他引用也为空。内置单元格标题fieldDispl
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行的单元格 因此,您必须在这些行中添加
返回单元格
第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
}
else {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "switchCell", for: indexPath) as? SwitchCell else {return UITableViewCell()}
cell.configureCell(withFieldLabal: "Track")
return cell
}
第2节第1行:
if indexPath.row == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as? TextFieldCell else {return UITableViewCell()}
cell.configureCell(withPlaceHolder: "Location")
return cell
}
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
}
设置断点。通过。看看你的代码在做什么。顺便说一句-在创建单元格时,去掉那些
guard let
。只需将强制转换(as!
)用于正确的单元格类型。如果在开发过程中设置不正确,您希望代码崩溃,而不是返回空单元格。