Swift - JSQMessagesViewController 显示传入的头像电子邮件或用户名

Swift - JSQMessagesViewController Showing incoming avatar email or username

我正在开发一个聊天应用程序,我有这个

现在的问题是如何在演示中像这样在消息气泡顶部显示用户名? https://raw.githubusercontent.com/jessesquires/JSQMessagesViewController/develop/Screenshots/screenshot2.png

我正在寻找 override func collectionView(collectionView: JSQMessagesCollectionView!, senderDisplayNameDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! 但我认为这不可能。您认为这里的解决方案是什么?谢谢!

为了查看传入消息顶部的用户名,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
}

以上代码对我有用,希望对你也有用。欲了解更多信息,请参阅演示

https://github.com/jessesquires/JSQMessagesViewController

我想你只是失踪了。

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
}

这就是大多数人的想法让我知道是否有帮助