title: Swift: Pagination in PFQueryTableViewController
body: <p>I'm having trouble figuring out how Parse's (supposedly built in) pagination works. I have done hours of research, and almost everything I find is written in Objective-C, and when I try to convert it to Swift the app does not run. </p>
  
  <p>I am running a PFQueryTableViewController to load videos in MPMoviePlayerControllers.</p>
  
  <p>First, I'm unsure what to put in <code>numberOfRowsInSection</code>. Parse's AnyPic tutorial in Obj-C suggests to <code>return objects.count * 2 + (self.paginationEnabled ? 1 : 0)</code>. To be frank, I have no idea what that second part means, but either way, if I return <i>anything</i> in <code>numberOfRowsInSection</code>, such as <code>objects.count</code>, the app crashes immediately. </p>
  
  <p>If I simply leave out <code>numberOfRowsInSection</code>, the first page of videos is completely successful. Once I scroll down all the way however, I get to the "Load More..." cell which was added by Parse automatically. Then the app freezes. The log simply reads <code>(lldb)</code> during this crash. </p>
  
  <p>I suspect that there is not a cell loaded even though this other cell is appearing, but again I'm unsure what to do since whatever I put in <code>numberOfRowsInSection</code> crashes the app.</p>
  
  <p>Next, I'm totally at a loss of what to put in <code>cellForNextPageAtIndexPath</code>. Parse's tutorial suggests:</p>
  
  <pre><code>PAPLoadMoreCell *cell = [tableView dequeueReusableCellWithIdentifier:LoadMoreCellIdentifier];
  if (!cell) {
      cell = [[PAPLoadMoreCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LoadMoreCellIdentifier];
      cell.selectionStyle =UITableViewCellSelectionStyleNone;
      cell.hideSeparatorBottom = YES;
      cell.mainView.backgroundColor = [UIColor clearColor];
  }
  return cell;
  </code></pre>
  
  <p>I am unable to mock this code in Swift, specifically the third line. I have tried running the app, skipping the if-statement, to no success. 
  Perhaps this method needs to be complete in order for the pagination to work?</p>
  
  <p>That is basically it as far as my questions, but here are some of the endeavors I've taken trying to solve this.</p>
  
  <p>In <code>cellForRowAtIndexPath</code>, I tried the following:</p>
  
  <pre><code>if indexPath.row == objects.count - 1 { //Does NOT work if '-1' is omitted
                                          //Extremely glitchy
          println("Load next page")
          loadNextPage()
  
      }
  </code></pre>
  
  <p>This is moderately successful. It actually successfully loads more pages when the indexPath is close to the bottom. However, it is very glitchy. It crashes occasionally, saying <code>fatal error: Array index out of range (lldb)</code> This seems to be if I scroll fast enough to actually view the "Load More..." cell, so it seems like the same problem as above.</p>
  
  <p>If somebody can help me with this, I will be (almost) forever thankful.</p>
  
  <p><strong>Edit:</strong> Code included for how I am working with data.</p>
  
  <p>Here is my query for the table:</p>
  
  <pre><code>override func queryForTable() -&gt; PFQuery! { //Same code as when normally loaded videos
  
      var query = PFQuery(className:"post")
      query.orderByDescending("createdAt")
      return query
  
  }
  </code></pre>
  
  <p>The important aspects of the "post" class are a videoFile and label. These are successfully loaded into PFTableViewCell.</p>
  
  <p>Next, how I handle the cellForRowAtIndexPath:</p>
  
  <pre><code>override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -&gt; PFTableViewCell! {
  
      super.tableView.cellForRowAtIndexPath(indexPath)
  
      let myCell = tableView.dequeueReusableCellWithIdentifier("tableViewCell", forIndexPath: indexPath) as TableViewCell
  
      println("Current indexPath: \(indexPath.row)")
  
      if indexPath.row == objects.count - 1 {
          println("Last current object")
          //loadNextPage()
      }
  
      //Parse: Assigning values(url, user, count) to cells
      let vidFile = object.valueForKey("videoFile") as PFFile
      let vidStr = vidFile.url
      let vidURL = NSURL(string: vidFile.url)
      myCell.videoURL = vidURL
  
      myCell.usernameLabel.text = object.valueForKey("user") as? String
  
      return myCell
  
  }
  </code></pre>
  
  <p>This all works fine. Interestingly, <code>println("Current indexPath: \(indexPath.row)")</code> will not print ANYTHING when the "Load More" cell from Parse enters the view, which is making me suspicious.</p>
  
  <p>I have some methods that make sure not to load a new video unless the cell is taking up a majority of the view. This would cause a crash if it tried to load a video in the "Load More" cell, but I have implemented checks to disallow this and it still crashes when the cell comes into view. </p>
  
  <p>For example:</p>
  
  <pre><code>if indexPathToLoad.row != objects.count { 
          //Display video at proper indexPath
          cell.displayVideo()
      }
  
      else { // Does not fire
          println("Do not try to load new video: Load next page")
      }
  </code></pre>