2

Here's my code for simply displaying messages in a collection view (using jsq messages view controller).

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) ->

    Int {
        if let count = fetchedResultsControler.sections?[0].numberOfObjects {

            return count
        }
        return 0
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
    return cell
}

override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
    let msg : Mesages = fetchedResultsControler.objectAtIndexPath(indexPath) as! Mesages
    switch (msg.fromID!) {

    case(friend!.id!) : let messageData = JSQMessage(senderId: friend!.id!, displayName: friend!.id!, text: msg.text)
    return messageData

    default :
        let messageData = JSQMessage(senderId: userdefaults.objectForKey("FBid") as! String, displayName: userdefaults.objectForKey("FBid") as! String, text: msg.text)
    return messageData
    }
}

override func collectionView(collectionView: JSQMessagesCollectionView!,      messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {

let msg : Mesages = fetchedResultsControler.objectAtIndexPath(indexPath) as! Mesages

switch (msg.fromID!) {

case(friend!.id!) : return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor.cyanColor())

default :
    return JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImageWithColor(UIColor.orangeColor())

}

}
override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource!{
    return nil
}

Please don't put much thought into the code I have (all it does is grab messages from fetch results controller and displays them in a collection view). One thing I am stuck on is how to "Slide" the entire collection view to show the timestamp of each message. That said, I don't expect the algorithm for the timestamps, I already have the timestamps stored for each message. I'm only confused on the concept of sliding the collection view. I can't find anything on it. I've tried plenty of documentation. Thanks!

Mirza Sisic
  • 2,401
  • 4
  • 24
  • 38
Ryan
  • 969
  • 1
  • 6
  • 19
  • Please check the image above for more clarity. Image named : "Slide to show timestamp" – Ryan Sep 05 '16 at 05:00
  • i had once this requirement. I did like this. Placed a table view just behind the list of cells and displayed the times with right aligned on that. Now added pan gesture to self.view and on recognizing of it, i changed the x position of the list of chats by changing its leading and trailing constraint. and animating on gesture recognizer ended. You can also use one more thing. http://cocoadocs.org/docsets/SPXRevealableView/0.2/Categories/UITableView+SPXRevealAdditions.html – Mahesh Agrawal Sep 05 '16 at 05:09
  • Any luck on this? looking for something similar – Walker Feb 03 '17 at 23:14
  • yah man! I ended up implementing the chatto framework for messaging. They have this as well as many other features that JSQ lacks. Notably Pagination. They're not known for their good docs, but there's a good example to refer to. Sorry for the late reply, hope that helps ! – Ryan Feb 13 '17 at 18:47

0 Answers0