Alan
Alan

Reputation: 9471

Can't get my custom UITableViewCell to show

I am using a TableView xib and all the delegates and datasource seem to be running fine. If I set self.textLabel.text, it displays the generic number of tableviews correctly, but I need to have my custom TableViewCell showing.

I created a HistoryCell.xib that has just a tableviewcell in it. I created a UITableViewCell class "HistoryCell.h/HistoryCell.m" and it is set as the file owner of HistoryCell.xib. I connected the UILabels to the HistoryCell.h UILabel statusLabel UILabel nameLabel UILabel timeLabel

In my main ViewController class int he cellForRowAtIndexPath

I am putting in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"historyCellType";
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[HistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.nameLabel.text = @"Donald Duck";

return cell;

}

What am I doing wrong?

Thanks!

Upvotes: 2

Views: 3533

Answers (4)

Bala krishna
Bala krishna

Reputation: 39

- (UITableViewCell *)tableView:(UITableView *)tableView 
       cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellidentifier=@"cell1"; 
    customcell *cell =
      (customcell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];

    if (cell==nil)  {

    [tableView registerNib:[UINib nibWithNibName:@"Customcell" bundle:nil] 
               forCellReuseIdentifier:cellidentifier]; 

    cell =
     (customcell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier 
                 forIndexPath:[NSIndexPath indexPathForItem:indexPath.row 
                                                  inSection:indexPath.section]];   
    }   

    return cell; 
}

Upvotes: 0

veddermatic
veddermatic

Reputation: 1082

You need to deserialize the actual XIB data:

HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *cellnib = [[NSBundle mainBundle] loadNibNamed:@"HistoryXIBName" owner:self options:nil];
    cell = (HistoryCell *)[cellnib objectAtIndex:0];
}

Otherwise it's just using a "Default" cell.

EDIT: Also, if you are using storyboards, dynamic cell prototypes remove the need to create cells from NIB files (in case that's an option for you.)

EDIT The 2nd: You can try this as well (this assumes you are using ARC):

HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    HistoryCell *aNewCell = [[HistoryCell alloc] initWithNibName:@"HistoryXIBName" bundle:nil];
    cell = (HistoryCell *)*aNewCell.view;
}

There's another method using outlets to your cell in the containing view's XIB file that's a bit more involved that I used to use when iOS4 was current. If the edited version isn't' working for you, I'll dig out an old project and remember how I did it.

Upvotes: 4

rdelmar
rdelmar

Reputation: 104082

You should register the nib like this (probably in viewDidLoad):

[self.tableView registerNib:[UINib nibWithNibName:@"HistoryCell" bundle:nil ] forCellReuseIdentifier:@"historyCellType"];

Then in your cellForRowAtIndexPath method, use this new way where you don't need the if cell == nil clause:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"historyCellType" forIndexPath:indexPath];

cell.nameLabel.text = @"Donald Duck";

return cell;

}

Upvotes: 8

Anupdas
Anupdas

Reputation: 10201

When you use tableViewCell with xib, while instantiation load from xib.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

    static NSString *CellIdentifier = @"historyCellType";
    HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"HistoryCell"
                                         owner:nil
                                       options:nil]lastObject];
    }

    cell.nameLabel.text = @"Donald Duck";

    return cell;

    }

Upvotes: 0

Related Questions