Search code examples
iphoneobjective-cxcodeios5uitableview

UITableView Custom Cell Throwing SIGABRT Error


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?


Solution

  • I'm not entirely sure what I have done to resolve my problem but It is now working correctly.

    Here is what I currently have in my ViewController.m

    Note: The ResultsCustomCell.h and .m are the same as above.

    #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";
    
        // Make sure there are no quotations (") around the cell Identifier.
        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
    

    I then made sure the following things where correct:

    • The table view cell has the correct custom class (ResultsCustomCell).
    • The cell Identifier for the UITableViewCell was correct in Interface Builder.
    • The UILabel in interface builder was correctly linked to the IBOutlet.
    • The UITableView view had the correct controller (ViewController)

    After using this code and checking all of the above it appears to work.