2013-11-26 91 views
0

我在视图控制器中有一个UITableView。表视图使用名为UserFavoritesCell的自定义单元格。当引用代码这个小区,我得到以下警告:与自定义UITableViewCell不兼容的指针类型警告

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]; 
} 
+1

为什么你使用这个单元并且你没有使用实际的数据源来获取gaugeID?这种方法实际上会重新创建或从队列中调用单元格,以检索数据中已存在的属性,我猜测? – Astri

回答

0

我不是100 %肯定,但你尝试铸造细胞?

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

您写道:

由于UserFavoritesCellUITableViewCell一个子类,我不知道为什么我收到此警告。有任何想法吗?

因为尽管每一个橘子是一种水果,不是每一个果实是橙色...

cellForRowAtIndexPath:只知道表包含UITableViewCell S(水果)。如果您知道返回细胞是UserFavoritesCell(橙色),那么你可以断言,随着铸造:

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

没有断言(以及编译器相信你得到它的权利)编译器只知道它有一个UITableViewCell,因此警告。

相关问题