Objective-c – UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

iphoneobjective cuitableview

import

I am trying to implement a custom cell to use in my tableview

This is my custom cell interface class

@interface CustomCell : UITableViewCell {
    IBOutlet UILabel *nameLabel;
    IBOutlet UITextView *inputText;
    IBOutlet UIImageView *image;
    IBOutlet UIButton *btnBuy;
}

@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UITextView *inputText;
@property (nonatomic, retain) IBOutlet UIImageView *image;
@property (nonatomic, retain) IBOutlet UIButton *btnBuy;

@end

This is my view controller

#import "CustomCellProjectViewController.h"

@implementation CustomCellProjectViewController

@synthesize nameLabel, inputText, image, btnBuy, listData;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    image = [[UIImageView alloc] init];
    btnBuy = [[UIButton alloc] init];
    inputText = [[UITextView alloc] init];
    nameLabel = [[UILabel alloc] init];

    NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:image, @"image", inputText, @"text", nameLabel, @"label", btnBuy, @"button", nil];

    NSArray *array = [[NSArray alloc] initWithObjects:row1, nil];
    self.listData = array;

    [row1 release];
    [array release];
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    self.listData = nil;
}


- (void)dealloc {
    [listData release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    return [self.listData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for (id oneObject in nib)
            if ([oneObject isKindOfClass:[CustomCell class]]) {
                cell = (CustomCell *)oneObject;
            }
    }
    return cell;
}

- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *) indexPath{
    NSUInteger row = [indexPath row];
    if (row == 0)
        return nil;
    return indexPath;
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    NSString *rowValue = [listData objectAtIndex:row];
    NSString *message = [[NSString alloc] initWithFormat:@"You selected %@", rowValue];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selection" message:message delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
    [alert show];
    [message release];
    [alert release];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat) tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kTableViewRowHeight;
}

@end

But I am getting an error:

UITableView dataSource must return a
cell from
tableView:cellForRowAtIndexPath:

cell is remaining nil

Can anyone help me please?

Best Answer

As for me, I will write codes for CustomCell manually. Then, I can use the cell in -cellForRowAtIndexPath as following.

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil)
    { 
        cell = [[[CustomCell alloc] initByMyWayWithIdentifier:CustomCellIdentifier] autorelease];
    }

    // assign necessary information to cell

    return cell;
}
Related Topic