2

我正在使用JSQMessagesViewController. 但是存在消息气泡在collectionView.

这是一个屏幕截图

在此处输入图像描述

这是代码:

import UIKit

class mDetailContainerViewController: JSQMessagesViewController, JSQMessagesCollectionViewDelegateFlowLayout{


    var userName = ""
    var messages = [JSQMessage]()
    let incomingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor.lightGrayColor())
    let outgoingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor.greenColor())

    override func viewDidLoad() {
        super.viewDidLoad()

        self.userName = "iPhone"

        for i in 1...10{

            var sender =  (i%2 == 0) ? "Syncano" : self.userName
            var message = JSQMessage(senderId: sender, displayName: sender, text: "Text")
            self.messages += [message]


        }

        self.collectionView.reloadData()
        self.senderDisplayName = self.userName
        self.senderId = self.userName
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
        var data = self.messages[indexPath.row]
        return data
    }

    override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
        var data = self.messages[indexPath.row]
        if (data.senderId == self.senderId){

            return self.outgoingBubble

        }else{

            return self.incomingBubble

        }
    }

    override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
        return nil
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.messages.count
    }

    override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: NSDate!) {
        var message = JSQMessage(senderId: senderId, displayName: senderDisplayName, text: text)
        messages += [message]
        self.finishSendingMessage()
    }

    override func didPressAccessoryButton(sender: UIButton!) {

    }

我在互联网上搜索了一些解决方案,但所有说明都很复杂。我什么都没弄清楚。此外,我不确定我是否找到了解决方案。如果有人会简单地解释解决方案,我会很高兴。

注意:JSQMessagesViewController显示在容器视图中。

最后,如何使用本地化更改发送按钮标题和文本字段占位符。

谢谢。

4

4 回答 4

4

我不熟悉这个 SDK,但我认为这是因为这两行

let incomingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor.lightGrayColor())
let outgoingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor.greenColor())

incomingMessagesBubbleImageWithColor。将出局气泡设置为JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImageWithColor(如果存在)

而且我以前从未使用过此消息类,但我相信您与绿色文本的对齐问题可能会通过类似...

outgoingBubble.textLabel.textAlignment = NSTextAlignment.Center
于 2015-08-04T13:32:57.113 回答
2

要修复气泡位置,请使用此代码删除头像图像;

self.collectionView.collectionViewLayout.incomingAvatarViewSize = CGSizeZero
self.collectionView.collectionViewLayout.outgoingAvatarViewSize = CGSizeZero
于 2015-08-04T15:08:41.243 回答
1

对于将来遇到此问题的任何人,请检查您的代码是否如下所示:

override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {

    let data = messages[indexPath.row]

    if data.senderId == currentUser.objectId! {
       return incomingBubble
    } else {
        return outgoingBubble
    }
}

它应该是这样的:

override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {

    let data = messages[indexPath.row]

    if data.senderId == PFUser.currentUser()!.objectId! {
        return outgoingBubble
    } else {
        return incomingBubble
    }
}

为我修好了!

于 2016-07-11T13:34:39.650 回答
1

斯威夫特 3中:

对于将文本居中气泡内的特定问题:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell

    cell.textView.textAlignment = .center

    return cell
}
于 2016-12-19T14:48:38.810 回答