PFQueryTableViewController Swift – load next page

I was having some issues using PFQueryTableViewController with Parse loading the next page using didselectrowatindexpath. The error was NSIndexOut of Range when selecting the load next page cell.

Here was the fix:


// Initialize the PFQueryTable tableview
    override init(style: UITableViewStyle, className: String?) {
        super.init(style: style, className: className)
    }
    
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        parseClassName = "yourClass"
        pullToRefreshEnabled = true
        paginationEnabled = true
        objectsPerPage = 11
        
    }
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
        
        
        
        let cellIdentifier = "statusCell"
        var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? statusCellClass
        if cell == nil {
            cell = statusCellClass(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
            }
        
        // Extract values from the PFObject to display in the table cell
        if let object = object {
          cell!.status.text = (object["status"] as! String)
        // Date for cell subtitle
            var dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd"
                let dateForText = object.createdAt as NSDate!
                cell!.created.text = dateFormatter.stringFromDate(dateForText)
        }
        return cell!
    }
    
    

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
        //if the index is out of range 11, [0-10] make that cell load next page
        if (indexPath.row == self.objects!.count){
            loadNextPage()
        }
        else{
            var targetObject = objectAtIndexPath(indexPath)!
            rowObjectID = targetObject.objectId as String!
            NSLog("Row Pressed")
            NSLog("%@", targetObject)
            NSLog("%@", rowObjectID)
            performSegueWithIdentifier("editStatus", sender: self)
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *