Question

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;
 }
Was it helpful?

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

OTHER TIPS

If you designed your UITableView cell in a XIB (which it sounds like you did), then you can't use the alloc init paradigm to initialize your object. You have to use:

cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCellXibFile" 
                                     owner:nil 
                                   options:nil] objectAtIndex:0]

Swift Version (prior to 1.2):

import UIKit

class JPUsersTableViewController: PFQueryTableViewController {

override init!(style: UITableViewStyle, className: String!) {
    super.init(style: style, className: className)
    textKey = "username"
    pullToRefreshEnabled = true
    paginationEnabled = true
    objectsPerPage = 25
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func viewDidLoad() {
    super.viewDidLoad()

    title = "Users"

    tableView.registerClass(PFTableViewCell.self, forCellReuseIdentifier: kTableViewCellIdentifier)
    tableView.separatorInset.right = tableView.separatorInset.left
    tableView.tableFooterView = UIView(frame: CGRectZero)
    view.backgroundColor = kbackgroundColor

    let returnIcon = UIBarButtonItem(image: kNavBarReturnIcon, style: .Plain, target: navigationController, action: "popViewControllerAnimated:")
    returnIcon.tintColor = kToolbarIconColor
    navigationItem.leftBarButtonItem = returnIcon

    tableView.reloadData()
    addPullToRefresh()
}

override func queryForTable() -> PFQuery! {
    let query = PFUser.query()
    query.whereKey("username", notEqualTo: PFUser.currentUser().username)
    query.orderByAscending("username")

    //if network cannot find any data, go to cached (local disk data)
    if (self.objects.count == 0){
        query.cachePolicy = kPFCachePolicyCacheThenNetwork
    }

    return query
}

// MARK: - Navigation

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as PFTableViewCell
    cell.textLabel?.text = object["username"] as? String

    if let profileImage = object["profileImage"] as? PFFile {
        cell.imageView.file = profileImage
    }
    else {
        cell.imageView.image = kProfileDefaultProfileImage
    }

    cell.textLabel?.font = UIFont(name: kStandardFontName, size: kStandardFontSize)
    cell.textLabel?.textColor = UIColor.whiteColor()
    cell.backgroundColor = kbackgroundColor

    return cell
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 50
}  
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top