0

我開發一個聊天應用程序,我現在有這個enter image description here斯威夫特 - JSQMessagesViewController顯示來電頭像的電子郵件或用戶名

的問題是我怎麼能顯示在消息頂部的用戶名氣泡這樣的演示? https://raw.githubusercontent.com/jessesquires/JSQMessagesViewController/develop/Screenshots/screenshot2.png

我在尋找override func collectionView(collectionView: JSQMessagesCollectionView!, senderDisplayNameDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource!但我認爲這是不可能的。你認爲這裏的解決方案是什麼?謝謝!

+0

由於缺乏靈活性,如果你需要一個完整的標誌消息應用程序,我不會推薦JSQ,我們最終編寫我們自己viewController這非常棘手... –

回答

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 
} 

上面的代碼適用於我,我希望它也適用於你。欲瞭解更多信息,你可以看到演示

https://github.com/jessesquires/JSQMessagesViewController

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 
} 

這就是拋出大多數人讓我知道這是否有助於