如何在一个 tableView 上提供 2 个以上的自定义单元格进行聊天

How to give more than 2 custom cell on one tableView for chat

用于聊天的聊天图像 ui 有 3 种类型,即文本、图像和旋转木马。我需要为一个 tableView 制作 3 个自定义单元格吗?该怎么做?

在单元格中

if Condition1 {
    let cell : CellOne! = tableView.dequeueReusableCell( withIdentifier: "CellOne") as? CellOne
    return cell
}else if Condition2{
    let cell : CellTwo! = tableView.dequeueReusableCell( withIdentifier: "CellTwo") as? CellTwo
    return cell
}else{
    let cell : CellThree! = tableView.dequeueReusableCell( withIdentifier: "CellThree") as? CellThree
    return cell
}

是的,您必须创建三个自定义单元格,以便使用第三方或 tableview 单元格内的集合视图。

例如:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cellIncomming = tableView.dequeueReusableCell(withIdentifier: "IncommingChatCell") as! IncommingChatCell
  let cellOutgoing = tableView.dequeueReusableCell(withIdentifier: "OutgoingChatCell") as! OutgoingChatCell

  let chatInfo = chatDataSourse[indexPath.row]
  if chatInfo.user == "receiver" {
    cellIncomming.chatLabel.text = chatInfo.chatString
    return cellIncomming
  }else {
    cellOutgoing.chatLabel.text = chatInfo.chatString
    return cellOutgoing
  }
}