JSQMessageViewController - How to set collection cell as input or output cell?

enter image description here

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

      

+3


source to share


3 answers


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

+3


source


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

      

0


source


For anyone looking for this, the current solution is to override the method isOutgoingMessage()

on yours JSQMessagesViewController

, not the approved one.

0


source







All Articles