I am trying to create a custom cell for my UITableView. I am using Xcode 4.2 and using the storyboard along with ARC.
I have created a class to represent the custom cell like so:
ResultsCustomCell.h
#import <UIKit/UIKit.h>
@interface ResultsCustomCell : UITableViewCell
{
IBOutlet UILabel *customLabel;
}
@property (nonatomic, retain) IBOutlet UILabel* customLabel;
@end
ResultsCustomCell.m
#import "ResultsCustomCell.h"
@implementation ResultsCustomCell
@synthesize customLabel;
@end
I have then implemented the UITableView method in my view controller as follows: ViewController.m
#import "ViewController.h"
#import "ResultsCustomCell.h"
@implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
// Set the number of items in the tableview to match the array count
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
// Populate TableView cells with contents of array.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
myCell.customLabel.text = @"helloWorld";
return myCell;
}
// Define height for cell.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
@end
The application builds successfully but then crashes instantly and give me the following error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
What part am I missing out?
This is an irritating problem because the debugger gives you no clue as to what happened. It will break on the exception and then simply shut down if you click Continue Execution.
This drove me nuts for 30 minutes until I right clicked on a UILabel in the custom cell that was giving me problems, which revealed the answer: One of the controls in your cell has (or had, as you apparently fixed it, perhaps without noticing) a spurious outlet. By "spurious outlet" I mean one that is wired to an IBOutlet property that no longer exists in your class. In my case I had changed the name of the property and rewired it, but had forgotten to remove the old referencing outlet connection.
As soon as I removed the offending outlet the problem went away.
You forgot to create a new UITableViewCell
when it's not dequeued
// Populate TableView cells with contents of array.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (myCell == nil) {
myCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
myCell.customLabel.text = @"helloWorld";
return myCell;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With