I would expect a very easy way to know how many messages are currently shown with JSQMessagesViewController. Couldn't find any directly asking the controller. Is there?
Asked
Active
Viewed 260 times
3
-
1How are you passing the data in `JSQMessagesViewController ` – Rajat Sep 03 '15 at 16:02
-
@Rajatp I'm using this code: https://github.com/relatedcode/NotificationChat – Idan Sep 03 '15 at 16:08
2 Answers
1
To get total number of available messages:
// If you don't have multiple sections, simply go get the number of message in the first section
NSInteger numberOfAvailableMessages = [self.collectionView numberOfItemsInSection:0];
Pay attention to call that after the data has been already loaded to the screen.
-
1It depends on the `timing` that you try to get it though. If the UICollectionView data hasn't been loaded at least once, off course there is nothing to count from it. Can you let me know about this? – Ducky Sep 14 '15 at 09:40
-
This is the correct answer. Sadly bounty was given automatically to the wrong answer (for some reason people vote for it). – Idan Sep 16 '15 at 11:09
-
Glad to hear that my answer was helpful. StackOverflow isn't perfect, no worries :) – Ducky Sep 16 '15 at 11:16
-1
Try this:
In query.limit, you can set the number of displayed messages. Even I was trying to get the grips with this, but saw an implementation from JesseHu on his chat example.
func loadMessages() {
if self.isLoading == false {
self.isLoading = true
var lastMessage = messages.last
var query = PFQuery(className: <your class name>)
query.whereKey(PF_CHAT_GROUPID, equalTo: groupId)// query params
if lastMessage != nil {
query.whereKey(PF_CHAT_CREATEDAT, greaterThan: lastMessage?.date)
}
query.includeKey(PF_CHAT_USER)
query.orderByDescending(PF_CHAT_CREATEDAT)
query.limit = 25
query.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
self.automaticallyScrollsToMostRecentMessage = false
for object in (objects as! [PFObject]!).reverse() {
self.addMessage(object)
}
if objects.count > 0 {
self.finishReceivingMessage()
self.scrollToBottomAnimated(false)
}
self.automaticallyScrollsToMostRecentMessage = true
} else {
println("Network error")
}
self.isLoading = false;
})
}
}

Skper11
- 61
- 10
-
I don't want to change how many messages I load but to check how many messages are currently showing on the view... – Idan Sep 13 '15 at 15:43