是否可以隐藏消息泡沫

时间:2016-03-17 00:09:05

标签: jsqmessagesviewcontroller

chat with system messages

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

1 个答案:

答案 0 :(得分:2)

我通过覆盖collectionView sizeForItemAtIndexPath函数并返回kJSQMessagesCollectionViewCellLabelHeightDefault的高度,然后为nilmessageBubbleImageDataForItemAtIndexPath <返回avatarImageDataForItemAtIndexPath来实现我的需要/ p>

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]
    }
}