swiftuitableviewpfquery

How to load more cells in UITableView SWIFT


I have a UITableViewController (instead of a PFQueryTableViewController) to display my query results and I have an array storing texts. Since the query would fetch huge amount of data, I would like my tableView to load more results once the user scroll to the bottom. There are many solutions out there but they're either in JSON or ObjectiveC and they seem really vague to me, as I am just a beginner.

class queryResultsViewController: UITableViewController {

var texts = [String]()


override func viewDidLoad() {
    super.viewDidLoad()

    let query = PFQuery(className: "allPosts")

    query.whereKey("userId", equalTo: (PFUser.currentUser()?.objectId)!)
    query.orderByDescending("createdAt")

    query.findObjectsInBackgroundWithBlock { (posts, error) -> Void in

        if let posts = posts {

            self.texts.removeAll(keepCapacity: true)

            for post in posts {

                self.captionOne.append(post["text"] as! String)

                self.tableView.reloadData()
            }
        }
    }
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return texts.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! theCell

    cell.TextView.text = texts[indexPath.row]

    return cell
}

Solution

  • To detect when the user has scrolled to the bottom of the UITableView, you can implement the UIScrollView delegate method scrollViewDidScroll:

    An example implementation (converted to Swift from: https://stackoverflow.com/a/5627837/3933375)

    override func scrollViewDidScroll(scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        let bounds = scrollView.bounds
        let size = scrollView.contentSize
        let inset = scrollView.contentInset
        let y = CGFloat(offset.y + bounds.size.height - inset.bottom)
        let h = CGFloat(size.height)
    
        let reload_distance = CGFloat(10)
        if(y > (h + reload_distance)) {
            print("load more rows")
        }
    }
    

    When this fires you can then download more results from parse, add them to the datasource of your UITableView and call reload data.

    Also, looking at your code you probably need to make a call to dispatch_async as you're trying to update the UI in a background block e.g.

    dispatch_async(dispatch_get_main_queue()) { () -> Void in
            self.tableview.reloadData()
        }
    



    Edit
    To load more results from Parse

    let query = PFQuery(className: "allPosts")
    
    query.whereKey("userId", equalTo: (PFUser.currentUser()?.objectId)!)
    query.orderByDescending("createdAt")
    
    query.limit = 50 // or your choice of how many to download at a time (defaults to 100)
    query.skip = 50 // This will skip the first 50 results and return the next limit after. If
    
    query.makeRequest......
    



    In your completion handler, make sure you append the results to the overall datasource (in your case texts), and call reload data.