【问题标题】:Property not found on object of type 'UITableViewCell'在“UITableViewCell”类型的对象上找不到属性
【发布时间】:2014-05-26 14:06:09
【问题描述】:

我有 2 个自定义 UITableViewCell,并且都有属性。 尽管NSLog(@"%@", cell.class) 表明两者都被正确创建,但我无法在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

我错过了什么?

【问题讨论】:

标签: objective-c ios7 uitableview


【解决方案1】:

您使用的是UITableViewCell 而不是IGBRealtyCell。尝试将其转换为 IGBRealtyCell 变量。

- (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;
}

【讨论】:

  • 但是objective-c不应该知道我的班级是IGBRealtyCell吗?它甚至记录正确的类。我不想创建另一个变量。而且 cell = (IGBRealtyCell *)[tableView dequeueReusableCellWithIdentifier:RealtyCellIdentifier] 也不起作用。
  • 不,方法 dequeueReusableCellWithIdentifier 正在返回您扩展的 UITableViewCell 对象。如果你需要你的类属性,你需要向下转换它。此外,检查单元格isKindOfClass 是否需要也是个好主意。另外请记住,您需要注册单元类,因为此方法用于获取可重复使用的单元。
  • 你能告诉我你是否使用:[self.tableView registerNib: forCellReuseIdentifier:];?您的网点是否在 xib 中连接?
  • 不,它们是 Storyboard 中的原型单元。问题是 cell = [tableView dequeueReusableCellWithIdentifier:RealtyCellIdentifier] 返回 IGBRealtyCell 对象。 “注册单元类”是什么意思?
猜你喜欢
  • 1970-01-01
  • 2014-04-27
  • 2012-03-04
  • 2023-03-22
  • 2019-06-25
  • 2021-06-30
  • 2012-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多