1

I'm trying to set custom colors for the chat bubbles, depending on the gender of the person who sent the message. Doing so for the outgoing bubble is easy, I just use a switch statement and check my gender dictionary for the value of the current user's uid. This is fine because the outgoing bubble will always be the same color.

For the incoming bubble, however, it's more difficult because it's a group chat, and the color/gender will be different depending on who is sending the message.

I think what I need is to get the index path of the messages, like in the class methods (such as the avatar function), where I can say

let message = messages[indexPath.row]

And then change the color based on that, with something like

private func setupIncomingBubble() -> JSQMessagesBubbleImage {

    // How to get indexPath to use in here????
    let message = messages[indexPath.row]
    let gender = genderDictionary.value(forKey: message.senderId)

    switch gender {
    case "male":
        return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor(hexString: "2573C5"))
    case "female":
        return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor(hexString: "E452CE"))
    default:
        return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor(hexString: "848484"))
    }
}

However I'm not sure how I can get this index path outside of the class methods. Is it possible, or will I need to take a separate approach?

EDIT:

private func setupIncomingBubble() -> JSQMessagesBubbleImage {

    let cell: JSQMessagesCollectionViewCell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
    let message: JSQMessage = self.messages[indexPath.item]

    let gender = genderDictionary.value(forKey: message.senderId)

    switch gender {
    case "male":
        return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor(hexString: "2573C5"))
    case "female":
        return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor(hexString: "E452CE"))
    default:
        return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor(hexString: "848484"))
    }

}

Errors on let cell and let message lines, "use of unresolved identifier indexPath"

KingTim
  • 1,281
  • 4
  • 21
  • 29

1 Answers1

0

Use index path using super keyword & it will inherits indexpath for CollectionView

 let cell: JSQMessagesCollectionViewCell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
 let msg: JSQMessage = self.messages[indexPath.item]
Jack
  • 13,571
  • 6
  • 76
  • 98
  • Thanks, I gave this a shot and got a few errors (unresolved identifier on indexPath) - I edited my question to show to full function I have now. – KingTim May 29 '17 at 16:03
  • hii @Jack have u worked with JSQMessagesViewController i need help from u – Dilip Tiwari Aug 07 '18 at 06:07
  • Jsqmessage is deprecated, they provide no longer support, search for another library – Jack Aug 07 '18 at 06:08