0

与系统消息聊天

我正在使用 JSQMessagesViewController 在我的 iOS 应用程序中实现聊天。我需要在屏幕中间显示一些系统消息(见附图)。我希望我可以通过使用消息底部标签而不显示消息气泡来实现这一点。但是我还没有找到隐藏消息气泡的方法。可能吗?谢谢。

4

1 回答 1

2

我通过重写collectionView sizeForItemAtIndexPath函数实现了我需要的并返回高度kJSQMessagesCollectionViewCellLabelHeightDefault,然后返回nil两者messageBubbleImageDataForItemAtIndexPathavatarImageDataForItemAtIndexPath

override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let userMessageCellSize = super.collectionView(collectionView, layout: collectionViewLayout, sizeForItemAtIndexPath: indexPath)
    let message = messages[indexPath.item]
    if message.type == MessageType.System {
        // for system message, only show bottom label, plus the top label when timestamp needs to be displayed
        // this will hide the avatar image and message bubble which are not needed for system messages.
        var newHeight: CGFloat = 0
        if (shouldDisplayTimestamp(indexPath)) {
            newHeight = kJSQMessagesCollectionViewCellLabelHeightDefault * 2
        } else {
            newHeight = kJSQMessagesCollectionViewCellLabelHeightDefault
        }
        return CGSizeMake(userMessageCellSize.width, newHeight)
    } else {
        return super.collectionView(collectionView, layout: collectionViewLayout, sizeForItemAtIndexPath: indexPath)
    }
}

override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
    let message = messages[indexPath.item]

    if message.type == MessageType.System {
        return nil
    }

    if message.senderId == senderId{
        return self.outgoingBubbleImageView
    } else {
        return self.incomingBubbleImageView
    }
}

override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
    let message = messages[indexPath.item]

    if message.type == MessageType.System {
        return nil
    }

    if let avatar = avatars[message.senderDisplayName] {
        return avatar
    } else {
        setupAvatarImage(message.senderDisplayName, imageUrl: message.profileImgUrl, incoming: true)
        return avatars[message.senderDisplayName]
    }
}
于 2016-03-17T04:53:49.757 回答