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

时间:2018-06-19 00:13:58

标签: ios swift uitableview

我正在使用动态UITableView,自定义UITableViewCells和内置的Right Detail单元格创建表单。我的tableview有3个部分。 tableview不会渲染我的任何自定义单元格的第二次出现,但它确实渲染第一次出现。我的自定义单元格的标识符是textFieldCell和switchCell。在第0行第0行使用自定义单元格可以很好地呈现,但是第1行第0行的单元格的下一个引用只显示一个空单元格。第2行第0行的自定义单元格也只显示一次。对此自定义单元格的另一个引用也是空的。每次调用内置单元格标题fieldDisplayCell都会呈现正常。

有谁知道tableview没有呈现自定义单元格的第二个引用的原因?我正在使用的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()
}

2 个答案:

答案 0 :(得分:1)

您似乎没有在第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
}

答案 1 :(得分:0)

您是否覆盖了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
}