I'm trying to figure out how to scroll to the bottom of the UITableView once the messages from the database load into it. I've tried using tableView.setContentOffset(CGPointMake(0, CGFloat.max), animated: true)
but my table goes from having all the messages in it to nothing at all. I've also tried
var iPath = NSIndexPath(forRow: messagesArray.count - 1, inSection: 0)
tableView.scrollToRowAtIndexPath(iPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
but I get a EXC_BAD_ACCESS code = 1
. Any help would be appreciated.
EDIT:
I'm putting this code in my viewDidAppear() function right after Parse finds the data from the query.findObjectsInBackgroundWithBlock(...), not sure if that makes a difference or not.
Here's the query code:
query.findObjectsInBackgroundWithBlock { (object, error) -> Void in
if (error == nil) {
for var i = 0; i < object!.count; i++ {
var messageObject = Messages()
messageObject.messageID = object![i]["messageID"] as! String
println("MESSAGE ID")
println(messageObject.messageID)
messageObject.messageText = object![i]["message"] as! NSString as String
println("MESSAGE TEXT")
println(messageObject.messageText)
messageObject.recipientID.append(object![i]["recipientID"] as! NSString as String)
println("RECIPIENT")
println(messageObject.recipientID[0])
messageObject.senderID = object![i]["senderID"] as! NSString as String
messageObject.timeStamp = object![i]["timeStamp"] as! NSString as String
println("TIMESTAMP")
println(messageObject.timeStamp)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.messagesArray.append(messageObject)
})
}
self.loadObjects()
self.tableView.reloadData()
self.tableView.setContentOffset(CGPointMake(0, CGFloat.max), animated: true)
}
}