Context
- Building a type of newsfeed
Problem
- I need to load more posts when the user scrolls to the last table view cell
- I have no idea where to start, how to implement, where in code
- So basically I need to call to server to download posts when user scrolls to last table view, append those posts to the bottom of the current table view
Here are the relevant code pieces (I think!)
class GeneralPostAreaController: UIViewController {
...
extension GeneralPostAreaController: UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return PostsCollection.postsFromParse.posts.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as! PostTableViewCell
cell.postImage.image = PostsCollection.postsFromParse.posts[indexPath.row].image
cell.postName.text = PostsCollection.postsFromParse.posts[indexPath.row].name
cell.postText.text = PostsCollection.postsFromParse.posts[indexPath.row].text
cell.loadAudio = PostsCollection.postsFromParse.posts[indexPath.row].audioObject.parseAudioData
return cell
}
}
And here is the code I use to get data from server (Parse). Excuse the horrible data structure, this is my first app!
class ParseQueryer: NSObject{
//HAVE TO RESTRUCTURE
var posts: [Post] = []
//this is not a very elegant way to reload table Data
func getPosts(selectedTableView: UITableView){
var query = PFQuery(className: "Post")
query.addDescendingOrder("createdAt")
query.limit = 10
query.findObjectsInBackgroundWithBlock {(result: [AnyObject]?, error: NSError?) -> Void in
self.posts = result as? [Post] ?? []
selectedTableView.reloadData()
for post in self.posts{
post.name = post["Name"] as? String
post.text = post["Text"] as? String
//println(post.text)
if let audioData = post["Audio"] as? PFFile {
audioData.getDataInBackgroundWithBlock({
(audioData: NSData?, error: NSError?) -> Void in
if (error == nil){
post.audioObject.parseAudioData = audioData
}
})
}
if let imgData = post["Photo"] as? PFFile {
imgData.getDataInBackgroundWithBlock({
(imageData: NSData?, error: NSError?) -> Void in
if (error == nil){
let image = UIImage(data: imageData!)
post.image = image
}
})
}
}
}
selectedTableView.reloadData()
I looked at the answer here but it doesn't make sense to me.