0

I have 2 custom UITableViewCell, and both have properties. Although NSLog(@"%@", cell.class) shows that both are being created correctly, I can't set my properties on tableView:cellForRowAtIndexPath:.


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

    if (indexPath.row != [self.realties count]) {
        cell = [tableView dequeueReusableCellWithIdentifier:RealtyCellIdentifier];

        NSLog(@"%@", cell.class);

        cell.realtyNameLabel.text = [[self.realties objectAtIndex:indexPath.row] adTitle];
        cell.realtyPriceLabel.text = [NSString stringWithFormat:@"R$%ld", [[self.realties objectAtIndex:indexPath.row] price]];
    }

    if (indexPath.row == [self.realties count]) {
        cell = [tableView dequeueReusableCellWithIdentifier:LoadMoreCellIdentifier];

        NSLog(@"%@", cell.class);
    }

    return cell;
}

@import UIKit;

@interface IGBRealtyCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *realtyImageView;
@property (weak, nonatomic) IBOutlet UILabel *realtyNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *realtyPriceLabel;

@end

What am I missing?

Larme
  • 24,190
  • 6
  • 51
  • 81
edmoks
  • 27
  • 7
  • 1
    `cell` is declared as `UITableViewCell` and not `IGBRealtyCell` or `YourSecondCustomCell`. Check how it's down there: http://stackoverflow.com/questions/14303832/uitableview-with-two-custom-cell-multiple-identifier – Larme May 26 '14 at 14:09

1 Answers1

1

You are using UITableViewCell instead of IGBRealtyCell. Try casting it to IGBRealtyCell variable.

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

    if (indexPath.row != [self.realties count]) {
        cell = [tableView dequeueReusableCellWithIdentifier:RealtyCellIdentifier];
        IGBRealtyCell *realtyCell = (IGBRealtyCell*)cell; 

        NSLog(@"%@", cell.class);

        realtyCell.realtyNameLabel.text = [[self.realties objectAtIndex:indexPath.row] adTitle];
        realtyCell.realtyPriceLabel.text = [NSString stringWithFormat:@"R$%ld",    [[self.realties objectAtIndex:indexPath.row] price]];
   }

    if (indexPath.row == [self.realties count]) {
        cell = [tableView dequeueReusableCellWithIdentifier:LoadMoreCellIdentifier];
        IGBRealtyCell *realtyCell = (IGBRealtyCell*)cell; 
        NSLog(@"%@", realtyCell.class);
    }

    return cell;
}
Apan
  • 1,027
  • 2
  • 10
  • 27
  • But wasn't objective-c supposed to know that my class is IGBRealtyCell? It even logs the right class. I didn't want to create another variable. And cell = (IGBRealtyCell *)[tableView dequeueReusableCellWithIdentifier:RealtyCellIdentifier] doesn't work either. – edmoks May 26 '14 at 14:24
  • No, method `dequeueReusableCellWithIdentifier` is returning `UITableViewCell` objects which you extends. If you need your class properties you need to downcast it. Also it would be good idea to check if the cell `isKindOfClass` you want. Also please remember that you need to register the cell class, because this method is use to get reusable cells. – Apan May 26 '14 at 14:30
  • Can you tell me if you use: `[self.tableView registerNib: forCellReuseIdentifier:];`? Are your outlets connected in xib? – Apan May 26 '14 at 14:36
  • No, they are prototype cells in Storyboard. The thing is that `cell = [tableView dequeueReusableCellWithIdentifier:RealtyCellIdentifier]` returns `IGBRealtyCell` object. What did you mean by "register the cell class"? – edmoks May 26 '14 at 16:42