Alexandre Ascone
Alexandre Ascone

Reputation: 21

Error using JSQMessagesViewController

Trying to make an Chat app on Iphone and I've a problem when integrating JSQMessagesViewController.

here is the full page code of my "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
    }
}

And this cause the following error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'ERROR: required method not implemented: -[JSQMessagesViewController collectionView:avatarImageDataForItemAtIndexPath:]

Can somebody help me please? thanks

Upvotes: 0

Views: 783

Answers (1)

Dan Leonard
Dan Leonard

Reputation: 3395

It is required so you just need to override it and pass back nil.

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

You can just add it to that same file. Then you should be good.

Upvotes: 1

Related Questions