0

尝试在 Iphone 上制作聊天应用程序,但在集成时遇到问题JSQMessagesViewController

这是我的“ConversationViewController”的完整页面代码

import UIKit
import JSQMessagesViewController

class ConversationViewController :  JSQMessagesViewController {
    var myIndexPath:Int!
    let outgoingBubble = JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImageWithColor(UIColor(red: 63/255, green: 173/255, blue: 169/255, alpha: 1.0))
    let incomingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor(red: 230/255, green: 231/255, blue: 236/255, alpha: 1.0))
    var messages = [JSQMessage]()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.setup()
        self.addDemoMessages()
        self.navigationItem.title = "\(myIndexPath)"
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func reloadMessagesView() {
        self.collectionView?.reloadData()
    }

}
//MARK - Setup
extension ConversationViewController {
    func addDemoMessages() {
        for i in 1...3 {
            let sender = (i%2 == 0) ? "Server" : self.senderId
            let messageContent = "Et eodem impetu Domitianum praecipitem per scalas itidem funibus constrinxerunt. \(i)"
            let message = JSQMessage(senderId: sender, displayName: sender, text: messageContent)
            self.messages += [message]

        }
        self.reloadMessagesView()
    }

    func setup() {
        self.senderId = UIDevice.currentDevice().identifierForVendor?.UUIDString
        self.senderDisplayName = UIDevice.currentDevice().identifierForVendor?.UUIDString
    }
}


//MARK - Data Source
extension ConversationViewController {

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

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

    override func collectionView(collectionView: JSQMessagesCollectionView!, didDeleteMessageAtIndexPath indexPath: NSIndexPath!) {
        self.messages.removeAtIndex(indexPath.row)
    }

    // sender type bubble
    override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
        let data = messages[indexPath.row]
        switch(data.senderId) {
        case self.senderId:
            return self.outgoingBubble
        default:
            return self.incomingBubble
        }
    }

    // cell text color
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell

        let message = messages[indexPath.item]
        if message.senderId == "Server" {
            cell.textView!.textColor = UIColor.blackColor()
        } else {
            cell.textView!.textColor = UIColor.whiteColor()
        }
        return cell
    }
}

这会导致以下错误:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“错误:未实现所需的方法:-[JSQMessagesViewController collectionView:avatarImageDataForItemAtIndexPath:]

有人可以帮我吗?谢谢

4

1 回答 1

1

它是必需的,因此您只需要覆盖它并返回 nil。

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

您可以将其添加到同一个文件中。那你应该很好。

于 2016-02-09T17:26:13.270 回答