当我添加多个测试消息时,屏幕上只显示其中两个。其他消息显然是因为我可以复制它们,但颜色根本没有显示。当我滚动时,会出现一个新的两个文本,但它仍然只有两个。下面是一些例子。
首次截图显示屏幕首次加载时的情况。第二个显示当我移动它时。第三个显示其他消息确实存在但不可见。有想法该怎么解决这个吗?另外,如何显示名称?在Swift中有这么好的指导吗?
以下是我使用的代码:
var messages = [JSQMessage]()
var incomingBubbleImageView = JSQMessagesBubbleImageFactory.incomingMessageBubbleImageViewWithColor(UIColor.jsq_messageBubbleLightGrayColor())
var outgoingBubbleImageView = JSQMessagesBubbleImageFactory.outgoingMessageBubbleImageViewWithColor(UIColor.jsq_messageBubbleGreenColor())
override func viewDidLoad() {
super.viewDidLoad()
self.sender = UIDevice.currentDevice().identifierForVendor?.UUIDString
messages += [JSQMessage(text: "hello", sender: self.sender)]
messages += [JSQMessage(text: "hello", sender: "other")]
messages += [JSQMessage(text: "hello", sender: self.sender)]
messages += [JSQMessage(text: "hello", sender: "other")]
messages += [JSQMessage(text: "hello", sender: self.sender)]
messages += [JSQMessage(text: "hello", sender: "other")]
reloadMessagesView()
}
func reloadMessagesView() {
self.collectionView?.reloadData()
}
这是委托方法的扩展代码:
extension TestJSQ {
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print(self.messages.count)
return self.messages.count
}
override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
let data = self.messages[indexPath.row]
return data
}
//覆盖func collectionView(collectionView:JSQMessagesCollectionView!,didDeleteMessageAtIndexPath indexPath:NSIndexPath!){ // self.messages.removeAtIndex(indexPath.row)
override func collectionView(collectionView: JSQMessagesCollectionView, bubbleImageViewForItemAtIndexPath indexPath: NSIndexPath) -> UIImageView {
let data = messages[indexPath.row]
switch(data.sender) {
case self.sender:
return self.outgoingBubbleImageView
default:
return self.incomingBubbleImageView
}
}
override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageViewForItemAtIndexPath indexPath: NSIndexPath!) -> UIImageView! {
return nil
}
}
任何帮助都将非常感谢!!!
答案 0 :(得分:0)
您需要在将消息添加到数据源后调用self.finishReceivingMessage()
(这将在调用collectionView.reloadData()
之前使布局无效)