-1

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.stack.imgur.com/tVt9j.png

PinkeshGjr
  • 8,460
  • 5
  • 41
  • 56
amaulana
  • 97
  • 1
  • 8

2 Answers2

2

in cellforrow

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
}
Yogesh Tandel
  • 1,738
  • 1
  • 19
  • 25
2

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
  }
}
Yogita J
  • 181
  • 1
  • 6
  • hi , i have one question . – amaulana Feb 13 '19 at 03:11
  • how if my response from api no have sender or receiver – amaulana Feb 13 '19 at 03:12
  • { "result": { "output": [ { "type": "text", "text": "Ini respons berbentuk teks", "speech": "Ini respons berbentuk teks" }, { "type": "text", "text": "Ini adalah teks yang dipakai sebagai dummy response untuk message type text", "speech": "Ini adalah teks yang dipakai sebagai dummy response untuk message type text" } ] } } – amaulana Feb 13 '19 at 03:13
  • Hi @amaulana tell your backend guys to provide anything(eg: userId or the sender/receiver etc), so that you can recognize that the chat comes from which side. In the above json i can not see any way to set chat bubbles. – Yogita J Feb 13 '19 at 05:27