Messagekit: MessagesDataSource methods are not triggering

Created on 15 Aug 2018  路  7Comments  路  Source: MessageKit/MessageKit

Hi,

for some reason MessagesDataSource methods are not triggering

here's the delegate code

        messageInputBar.delegate = self
        messagesCollectionView.messagesDataSource = self
        messagesCollectionView.messagesLayoutDelegate = self
        messagesCollectionView.messagesDisplayDelegate = self

here's my implementation for MessagesDataSource

extension ChatViewController : MessagesDataSource {
    func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
        return messages.count
    }

    func currentSender() -> Sender {
        return Sender(id: user.id!, displayName: user.name)
    }

    func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
        if indexPath.section % 3 == 0 {
            return NSAttributedString(string: MessageKitDateFormatter.shared.string(from: message.sentDate), attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 10), NSAttributedStringKey.foregroundColor: UIColor.darkGray])
        }
        return nil
    }

    func messageTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
        let name = message.sender.displayName
        return NSAttributedString(string: name, attributes:
            [.font : UIFont.systemFont(ofSize: 14),
             .foregroundColor: UIColor.black])
    }

    func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {

        let dateString = formatter.string(from: message.sentDate)
        return NSAttributedString(string: dateString, attributes: [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: .caption2)])
    }


    func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
        return messages[indexPath.section]
    }

}

What I am missing!?

question

Most helpful comment

@dotWasim It happens to all of us 馃槤 Going to close this issue, glad you got it resolved~

All 7 comments

@dotWasim Have you added messages your your message array and reloaded? Can you be more specific rather than "some of these methods". Which methods?

Sent with GitHawk

thanks @nathantannar4 for replay. Here's the full code. and none of MessagesDataSource methods are triggered.
I even reloaded the messagesCollectionView data using asyncAfter to be sure messages data are ready to be consumed.. I checked the messages using the inspector and I can see there are 17 messages loaded, so it's not data issue
ChatViewController.txt

@dotWasim sorry I'm not able to debug an entire controller for you. Please look at our ChatExample and your codebase to determine if there are any inconsistencies.

Sent with GitHawk

@nathantannar4 ummm, I am trying to fix it myself.. but I couldn't solve it yet!

@dotWasim, in viewDidLoad there is no call to super.viewDidLoad() in the ChatViewController.txt that you shared.

In the project I am using MessageKit in, if I remove the call to super.viewDidLoad() from my ViewControllers viewDidLoad then the messagesDataSource methods are not called and I get a blank view (black screen) instead of the CollectionView that should have been there.

Have you tried calling super.viewDidLoad()? I ask because there is a curious line in the viewDidLoad of the code you shared view.backgroundColor = .white and I am guessing that that is there because you were seeing a blank view instead of the CollectionView and just thought the background color was wrong.

Check this Stackoverflow answer for more about when to call super.viewDidLoad() : https://stackoverflow.com/a/40152226/924

@brandon-haugen you are right, this was my mistake! feeling stupid for wasting all that time on this small simple mistake!

@dotWasim It happens to all of us 馃槤 Going to close this issue, glad you got it resolved~

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yspreen picture yspreen  路  3Comments

robertoferraz picture robertoferraz  路  3Comments

brandon-haugen picture brandon-haugen  路  3Comments

ChandraPrakashJangid picture ChandraPrakashJangid  路  3Comments

emmanuelay picture emmanuelay  路  3Comments