Search code examples
iosswiftuitableviewchatcustom-cell

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


chat image for chat ui have 3 type which is text , image , and carousel . am i need to make 3 custom cell for one tableView and how to do that ?

https://i.sstatic.net/tVt9j.png


Solution

  • Yes you have to create three custom cell, for crousal either use third party or a collection view inside tableview cell.

    for eg:

    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
      }
    }