我正在使用JSQMessagesCollectionView來構建一個啓用聊天的應用程序。它到現在爲止工作得很好,因爲我開始注意到所有的消息氣泡都有第一個氣泡的寬度!如果消息比第一個泡泡更長,那麼文本會被剪切...(我查看了後臺發送和接收到的消息,文本似乎都很好......)JSQMessagesCollectionView(Message Bubble size all identical ..)
我想知道如果我在沒有正確設置JSQMessagesCollectionView某事錯了...我不能弄明白:(
這裏是我的代碼:
// Total number of Messages in Section
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return messages.count
}
// Message Data Model At IndexPath
override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
return messages[indexPath.item]
}
//messageBubbleImageDataForItemAtIndexPath
override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
let message = messages[indexPath.item]
if (outgoing(message)) {
print(outgoingBubble)
return outgoingBubble
} else {
return incomingBubble
}
}
override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
return avatarImageBlank // Return an empty avatar image for now
}
// Collection View text color and hyperlink
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
let message = messages[indexPath.item]
if (outgoing(message)) {
cell.textView!.textColor = UIColor.whiteColor()
} else {
cell.textView!.textColor = UIColor.blackColor()
}
// Under line links
let attributes : [String:AnyObject] = [NSForegroundColorAttributeName:cell.textView!.textColor!, NSUnderlineStyleAttributeName: 1]
cell.textView!.linkTextAttributes = attributes
return cell
}
// Usernames above bubbles at Indexpah
override func collectionView(collectionView: JSQMessagesCollectionView!, attributedTextForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> NSAttributedString! {
let message = messages[indexPath.item]
if (outgoing(message)) {
return nil
}
// Same as previous sender, skip, eg. 2nd message from other sender
if indexPath.item > 0 {
let previousMessage = messages[indexPath.item - 1]
if (previousMessage.senderId() == message.senderId()) {
return nil
}
}
// Otherwise, mark sender's name
return NSAttributedString(string:message.senderDisplayName())
}
//heightForMessageBubbleTopLabelAtIndexPath (Flow Layout)
override func collectionView(collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
let message = messages[indexPath.item]
// Sent by sendor, skip
if (outgoing(message)) {
return CGFloat(0.0)
}
// Same as previous sender, skip
if indexPath.item > 0 {
let previousMessage = messages[indexPath.item - 1]
if (previousMessage.senderId() == message.senderId()) {
return CGFloat(0.0)
}
}
return kJSQMessagesCollectionViewCellLabelHeightDefault
}
我定義的傳入和傳出的消息氣泡如下:
let incomingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor.lightGrayColor())
let outgoingBubble = JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImageWithColor(UIColor(red: 82.0/255.0, green: 181.0/255.0, blue: 1, alpha: 1.0))
你能詳細說一下嗎?你剛剛創建JSQMessages並將它們作爲dataSource?還是你仍然覆蓋cellForItemAtIndexPath並自己實現該單元格? –
@PaulLehn對不起,我花了很長時間回覆。我已經在上面編輯了我的答案。對於這個單元格,我使用了「JSQMessagesCollectionViewCell」,並且它沒有被自定義。 – Yanyan