1

I am using a Storyboard's prototype cell with a custom table cell class and the UILabels are nil in cellForRowAtIndexPath. Xcode's Identifier is correct, the cell is initialized, and the default UILabels (i.e. textLabel and detailTextLabel) are initialized but not the custom UILabels I added and setup IBOutlets for. A couple things I have tried:

  • I tried removing the registerClass calls in ViewDidLoad but that crashes since the cells need it to be initialized as the custom class GenericDetailCell.

  • viewWithTag returns nil

  • I tried iterating through all the subviews of UITableView to see if I was getting the wrong cell. The correct cell is getting returned by dequeueReusableCellWithIdentifier

Has anyone run into this?

@implementation PeopleGroupPickerViewController
{
    NSArray *people;
    NSArray *searchResults;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    people = [[DAL sharedInstance] getPeople:false];
    [self.tableView registerClass:[GenericDetailCell class] forCellReuseIdentifier:@"PersonCell"];
    [self.searchDisplayController.searchResultsTableView registerClass:[GenericDetailCell class] forCellReuseIdentifier:@"PersonCell"];

    // stackoverflow.com/questions/5474529
    [self.searchDisplayController setActive:YES];
    [self.searchDisplayController.searchBar becomeFirstResponder];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if(tableView == self.searchDisplayController.searchResultsTableView)
    {
        return 1;
    }
    else
    {
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView == self.searchDisplayController.searchResultsTableView)
    {
        return searchResults.count;
    }
    else
    {
        return people.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GenericDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell" forIndexPath:indexPath];

    Person_ *thisPerson;
    if(tableView == self.searchDisplayController.searchResultsTableView)
    {
        thisPerson = (Person_ *) searchResults[indexPath.row];
        NSLog(@"searchResultsTableView, %@",thisPerson.sName);
    }
    else
    {
        thisPerson = (Person_ *) people[indexPath.row];
        NSLog(@"UITableView, %@",thisPerson.sName);
    }
    Person_ *thisSpouse = [[DAL sharedInstance] getSpouse:thisPerson People:people];

    // cell.fieldName and cell.fieldValue are nil, cell is not nil
    cell.fieldName.text = thisPerson.sName;
    cell.fieldValue.text = thisSpouse.sName;

    return cell;
}

GenericDetailCell.h:

@interface GenericDetailCell : UITableViewCell

@property (nonatomic,weak) IBOutlet UILabel *fieldName;
@property (nonatomic,weak) IBOutlet UILabel *fieldValue;

@end
MarkF
  • 952
  • 2
  • 8
  • 25
  • try `GenericDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell"];` instead of `GenericDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell" forIndexPath:indexPath];` – Ryan Aug 25 '14 at 01:23
  • If your storyboard is correct you should not need to register the class in `viewDidLoad` - In fact, doing so is why you have the problem you have - you are getting an instance of that class, but it isn't connected to the storyboard – Paulw11 Aug 25 '14 at 02:14

1 Answers1

2

Seems to me you might be attempting to combine a UITableViewCell predefined style (e.g. UITableViewCellStyleSubtitle) with a custom cell.

In your storyboard, if you have selected a predefined style for your Prototype cell, other controls will not be recognised.

To remedy this problem, in the Attribute Inspector for your Prototype UITableViewCell, select "Custom" type. Then add into the Prototype cell all the controls you require, including those needed to replace the default controls previously added automatically into the predefined cell type.

andrewbuilder
  • 3,629
  • 2
  • 24
  • 46
  • 1
    That is correct. I was trying to use the "Right Detail" style (UITableViewCellStyleValue1) with a custom UITableViewCell class. I was trying to recognize the default UILabels in the custom class properties. Changing the style to "Custom", manually adding the UILabels, referencing them to the custom cell properties, and removing the registerClass calls in viewDidLoad resolved the issue. – MarkF Aug 25 '14 at 03:51
  • Good news. Do some testing with search, because I suspect that you may still need to register your custom cell class for your `searchResultsTableView`. – andrewbuilder Aug 25 '14 at 04:42
  • You saved me a lot of trouble by this heads up, I was just running into that issue. – MarkF Aug 25 '14 at 05:16
  • I am running into the same issue with the UILabel being nil in the UISearchResultsTableView when cellForRowAtIndexPath is called. When I don't call registerClass for the search results table, dequeueReusableCellWithIdentifier returns nil. When I call registerClass, dequeueReusableCellWithIdentifier returns a cell of the custom class whose UILabel properties are nil. – MarkF Aug 25 '14 at 05:25
  • Mark I wrote [this answer](http://stackoverflow.com/questions/23410353/search-bar-and-search-display-controller-in-table-view/23452670#23452670) some time ago in response to a question about setting up a search display controller programmatically. Try the code I have suggested under the third step. If that doesn't work let me know. – andrewbuilder Aug 25 '14 at 05:30