【问题标题】:Incompatible pointer types warning with custom UITableViewCell自定义 UITableViewCell 的不兼容指针类型警告
【发布时间】:2013-11-26 23:55:49
【问题描述】:

我在视图控制器中有一个 UITableView。表格视图使用一个名为 UserFavoritesCell 的自定义单元格。在代码中引用此单元格时,我收到以下警告:

Incompatible pointer types initializing UserFavoritesCell with an expression of UITableViewCell.

由于 UserFavoritesCell 是 UITableViewCell 的子类,我不确定为什么会收到此警告。有任何想法吗?谢谢!

标题:

@interface UserFavoriteCell : UITableViewCell

// properties...

@end

实施:

@implementation UserFavoriteCell

@synthesize lblFlow, lblHeight, lblLastUpdate, lblMainTitle, gaugeID;

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

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

在我的视图控制器中,我收到有关 UserFavoriteCell 实例化的警告,如下所示:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UserFavoriteCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // warning
    GaugeViewController *gvc = [[GaugeViewController alloc] init];
    [gvc setGaugeID:[cell gaugeID]];
    [self performSegueWithIdentifier:@"sgDetailFave" sender:self];
}

【问题讨论】:

  • 为什么你使用单元格而不是使用实际数据源来获取gaugeID?这个方法实际上会重新创建或从队列中调用单元格,只是为了检索我猜你的数据中已经存在的属性?

标签: ios objective-c uitableview


【解决方案1】:

我不是 100% 确定,但您尝试过投射单元格吗?

UserFavoriteCell *cell = (UserFavoriteCell *)[tableView cellForRowAtIndexPath:indexPath];

【讨论】:

    【解决方案2】:

    你写道:

    由于UserFavoritesCellUITableViewCell 的子类,我不确定为什么会收到此警告。有什么想法吗?

    因为虽然每个橙子都是水果,但并不是每个水果都是橙子......

    cellForRowAtIndexPath: 只知道表包含UITableViewCells(水果)。如果您知道返回的单元格是UserFavoritesCell(橙色),那么您可以通过强制转换来断言:

    ... cell = (UserFavoritesCell *) ...
    

    如果没有断言(并且编译器相信你会正确),编译器只能知道它有一个UITableViewCell,因此会发出警告。

    【讨论】:

      猜你喜欢
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多