BMN
BMN

Reputation: 8508

IOS5 - Custom UITableViewCell doesn't show content

I'm encountering an issue with my application, most specifically with a UITableView and its UITableViewCells. Here's the problem : I created a UITableView within a UIView, with custom UITableViewCell. I've two controllers involved :

In IB, I linked the items of the cell to the IBOutlets I manually created in the header file of the cell controller.

Here's the code for the ListCellController.h/m :

// .h
@class Item;

@interface ListCellController : UITableViewCell
@property (nonatomic, strong) Item *item;   
@property (nonatomic, strong) IBOutlet UILabel *itemName;
@property (nonatomic, strong) IBOutlet UILabel *dates;
@property (nonatomic, strong) IBOutlet UILabel *itemId;     
@end

// .m

// @synthetize stuff

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (!self) {
        return nil;
    }

    self.textLabel.adjustsFontSizeToFitWidth = YES;
    self.selectionStyle = UITableViewCellSelectionStyleGray;

    return self;
}

- (void)setItem:(Item *)item
{
    _item = item;

    self.itemName.text = _list.itemName;
    self.itemId.text = _list.itemId;
    self.dates.text = [NSString stringWithFormat:@"Du %@ au %@", _list.date, _list.date];
}

And here's the code for the ListController.h/m :

// .h
@interface ListController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    UITableView *cellList;
}
@property (strong, nonatomic) IBOutlet UITableView *cellList;
@end

// .m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"List cell";
    ListCellController *cell = [cellList dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[ListCellController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.item = [_items objectAtIndex:indexPath.row];
    cell.itemId.text = cell.item.itemId;
    cell.itemName.text = cell.item.itemName;
    cell.dates.text = [NSString stringWithFormat:@"From %@ to %@", cell.item.date, cell.item.date];
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cellList deselectRowAtIndexPath:indexPath animated:YES];
}

The problem I have is that when I launch the application, I see nothing on my cell. No content, no error, no nothing, blank cell.

Just to mention, I'm also using storyboarding here.

Has anyone already encountered this issue ? How could I solve it please ?

Thanks.

Upvotes: 0

Views: 2930

Answers (1)

DLende
DLende

Reputation: 5242

If you have custom cell on UITableView, you have to do something like this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"List cell";
    ListCellController *cell = (ListCellController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

   if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ListCellController" owner:self options:nil];
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (ListCellController *) currentObject;
                break;
            }
        }
    }

    cell.item = [_items objectAtIndex:indexPath.row];
    cell.itemId.text = cell.item.segmentCode;
    cell.itemName.text = cell.item.hotelName;
    cell.dates.text = [NSString stringWithFormat:@"Du %@ au %@", cell.item.checkin, cell.item.checkout];
    return cell;
}

Upvotes: 4

Related Questions