现在的问题是如何在演示中的消息气泡之上显示用户名? https://raw.githubusercontent.com/jessesquires/JSQMessagesViewController/develop/Screenshots/screenshot2.png
我正在寻找override func collectionView(collectionView: JSQMessagesCollectionView!, senderDisplayNameDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource!
但我认为这是不可能的。您认为这里的解决方案是什么?谢谢!
答案 0 :(得分:2)
为了查看传入消息的userName顶部,JSQ具有以下方法。
//MARK: To View usernames above bubbles
override func collectionView(collectionView: JSQMessagesCollectionView!, attributedTextForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> NSAttributedString! {
let message = messages[indexPath.item];
// Sent by me, skip
if message.sender() == sender {
return nil;
}
// Same as previous sender, skip
if indexPath.item > 0 {
let previousMessage = messages[indexPath.item - 1];
if previousMessage.sender() == message.sender() {
return nil;
}
}
return NSAttributedString(string:message.sender())
}
override func collectionView(collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
let message = messages[indexPath.item]
// Sent by me, skip
if message.sender() == sender {
return CGFloat(0.0);
}
// Same as previous sender, skip
if indexPath.item > 0 {
let previousMessage = messages[indexPath.item - 1];
if previousMessage.sender() == message.sender() {
return CGFloat(0.0);
}
}
return kJSQMessagesCollectionViewCellLabelHeightDefault
}
以上代码对我有用,我希望它也适合你。有关详细信息,请参阅演示
答案 1 :(得分:1)
我认为你只是缺席了。
override func collectionView(collectionView: JSQMessagesCollectionView?, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout?, heightForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
// Override point:
//
// Here we check for what our setting is for displaying the senderDisplayName
// if we dont want to display it we just return a height of 0.
// Then we check to see if (The current user)
// sent the message if so we return 0, because we know our own name,
// other wise we return the defualt height.
//
return kJSQMessagesCollectionViewCellLabelHeightDefault
}
这就是抛出大多数人的原因让我知道这是否有帮助