【问题标题】:Add multiple custom tableviewcells in UITableView section在 UITableView 部分添加多个自定义 tableviewcells
【发布时间】: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 只显示第一部分的内容?
  • 你有第二节标题吗?

标签: ios swift swift5 xcode12


【解决方案1】:

导致 Superveiw 约束的 imageView 是问题。删除约束后,将显示第二部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 2019-12-13
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多