这是我的代码,用于在集合视图中显示消息(使用jsq消息视图控制器)。
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
}
请不要过多考虑我的代码(它所做的只是从获取结果控制器中获取消息并在集合视图中显示它们)。我坚持的一件事是如何“滑动”整个集合视图以显示每条消息的时间戳。也就是说,我不期望时间戳的算法,我已经为每条消息存储了时间戳。我只是对滑动集合视图的概念感到困惑。我找不到任何东西。我已经尝试了很多文档。谢谢!