JSQMessageViewController - How to set collection cell as input or output cell?
Using the iOS JSQMessage subdirectory in this method;
collectionView: (JSQMessagesCollectionView *) collectionView cellForItemAtIndexPath: (NSIndexPath *) indexPath {..}
How do I set it up for use JSQMessagesCollectionViewCellIncoming
or JSQMessagesCollectionViewCellOutgoing
? I find this diffcult to find examples of how other apps are doing this
My code;
- (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
JSQMessagesCollectionViewCell *cell = (JSQMessagesCollectionViewCell*)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];
[cell.textView setDataDetectorTypes:UIDataDetectorTypeNone];
cell.textView.text = nil;
VICChatMessage <JSQMessageData> *messageData = (VICChatMessage*)[collectionView.dataSource collectionView:collectionView messageDataForItemAtIndexPath:indexPath];
cell.textView.attributedText = messageData.attributedText;
return cell;
}
source to share
I was able to resolve the issue. It has to do with the details of the sender.
It defaults to his JSQDefaultSender
, but my code only set it if he knew the sender; so I used a backup when the sender was not known.
The idea is to get
BOOL isOutgoingMessage = [messageSender isEqualToString:self.sender];
inside podfile: JSQMessagesViewController.m
Thus, it positions them both left and right.
In the end I had to do this in my code where I got my message to display
if (message.sender.remoteID)
{
senderID = @"JSQDefaultSender";
}
else
{
senderID = @"sender";
}
This works and solves my problem.
Thank you all very much
source to share
In the method, messageBubbleImageDataForItemAtIndexPath
you need to compare the sender of the message with your user. This sender is your user, please return outgoingMessagesBubbleImage
. If not, useincomingMessagesBubbleImage
- (id<JSQMessageBubbleImageDataSource>)collectionView:(JSQMessagesCollectionView *)collectionView messageBubbleImageDataForItemAtIndexPath:(NSIndexPath *)indexPath
{
JSQMessagesBubbleImageFactory *bubbleFactory = [[JSQMessagesBubbleImageFactory alloc] init];
Message *message = [your_messages objectAtIndex:indexPath.item];
if ([message.senderId isEqualToString:self.senderId]) {
return [bubbleFactory outgoingMessagesBubbleImageWithColor:[UIColor jsq_messageBubbleBlueColor]];
}
return [bubbleFactory incomingMessagesBubbleImageWithColor:[UIColor jsq_messageBubbleLightGrayColor]];
}
source to share