Search code examples
uitableviewparse-platformpfquery

PFQueryTableViewController not showing custom cells


I have a subclass of PFQueryTableViewController that I am trying to show in a container view (as a subview). My problem is that I cannot get the custom cells to show in the tableview. I have verified the following via debugging:

  • The tableview is being added to the parent view
  • The tableview is a PFQueryTableView Controller as it includes the default pull to refresh
  • The PFQuery is returning the correct number of objects
  • The CellForRowAtIndexPath method is being called and iterating through the correct number of times
  • The correct data from Parse is being passed to the different labels in the cells
  • The labels are connected via IBOulets in my subclass of UITableViewCell. When I am trying to access the labels it is working correctly as it accesses the subclass and label

I have everything working here correctly except that the cell actually shows up! What am I missing?

This is my cellForRowAtIndexPath code:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{

     static NSString *CellIdentifier = @"RoundCell";

    RoundCell*   cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

    if (cell == nil)
    {
        cell = [[RoundCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

     // get string values from Parse
     NSString * teeString =[object objectForKey:@"roundTee"];
     NSString* courseString = [object objectForKey:@"roundCourse"];
         NSString * courseString2 = [[courseString stringByAppendingString:@" - "]stringByAppendingString:teeString];
     NSString * dateString = [object objectForKey:@"roundDate"];
     NSString * scoreString = [object objectForKey:@"roundScore"];
    NSString * differentialString = [object objectForKey:@"roundDifferential"];


    cell.courseNameCell.text = courseString2;
    cell.dateCell.text = dateString;
    cell.scoreCell.text= scoreString;
    cell.differentialCell.text=differentialString;
     return cell;
 }

Solution

  • The correct method is to call the custom cell in cellForRowAtIndexPath.

    Check two basic things:

    1. on the storyboard and click on the cell in the Attributes inspector checks that the cell has the correct identifier

    enter image description here

    2. set the cellForRowAtIndexPath in this way:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{
    
     CustomCell *cell = (CustomCell * )[self.tableView dequeueReusableCellWithIdentifier:@"YOUR CELL NAME" forIndexPath:indexPath];
    

    So in your case try:

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{
    
         CustomCell *cell = (CustomCell * )[self.tableView dequeueReusableCellWithIdentifier:@"YOUR CELL NAME" forIndexPath:indexPath];
    
         NSString * teeString =[object objectForKey:@"roundTee"];
         NSString* courseString = [object objectForKey:@"roundCourse"];
             NSString * courseString2 = [[courseString stringByAppendingString:@" - "]stringByAppendingString:teeString];
         NSString * dateString = [object objectForKey:@"roundDate"];
         NSString * scoreString = [object objectForKey:@"roundScore"];
        NSString * differentialString = [object objectForKey:@"roundDifferential"];
    
    
        cell.courseNameCell.text = courseString2;
        cell.dateCell.text = dateString;
        cell.scoreCell.text= scoreString;
        cell.differentialCell.text=differentialString;
         return cell;
     }
    

    Do not forget to import the subclass of custom cell in your File.m

    #import "YourCustomCell.h"
    

    and set the cell in the identity inspector

    enter image description here