I am loading a custom cell into a table view, and notice that my cell is not being re-used correctly. I am using a NSFetchedResultsController to pull the results from Core Data.
I am loading the cell from a nib. The cell identifier is setup in interface builder. The cells appear to get re-used because I am not creating a new cell everytime I scroll the table. However, the data is not being displayed correctly on the cells.
// BeerCell.h
@interface BeerCell : UITableViewCell
@property (nonatomic, strong) IBOutlet UIImageView *beerImage;
@property (nonatomic, strong) IBOutlet UILabel *displayBeerName;
@property (nonatomic, strong) IBOutlet UILabel *displayBeerType;
@end
// BeerCell.m
@implementation BeerCell
@synthesize beerImage;
@synthesize displayBeerName;
@synthesize displayBeerType;
@end
// Code where i'm setting up the cells for the tableView
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"BeerCell";
BeerCell *cell = (BeerCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"BeerCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (BeerCell *) currentObject;
break;
}
}
[self configureCell:cell atIndexPath:indexPath];
}
return cell;
}
- (void)configureCell:(BeerCell *)cell
atIndexPath:(NSIndexPath *)indexPath
{
Beer *beer = (Beer *) [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.displayBeerName.text = beer.name;
}
Take the configureCell function call outside the if block.