Ios 与自定义UITableViewCell不兼容的指针类型警告
我在视图控制器中有一个UITableView。表视图使用名为UserFavoritesCell的自定义单元格。在代码中引用此单元格时,我收到以下警告:Ios 与自定义UITableViewCell不兼容的指针类型警告,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我在视图控制器中有一个UITableView。表视图使用名为UserFavoritesCell的自定义单元格。在代码中引用此单元格时,我收到以下警告: Incompatible pointer types initializing UserFavoritesCell with an expression of UITableViewCell. 由于UserFavoritesCell是UITableViewCell的子类,我不确定为什么会收到此警告。有什么想法吗?谢谢 标题: @interfac
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];
}
我不是百分之百确定,但你试过铸造细胞吗
UserFavoriteCell *cell = (UserFavoriteCell *)[tableView cellForRowAtIndexPath:indexPath];
你写道:
由于UserFavoritesCell
是UITableViewCell
的一个子类,我不确定为什么会收到此警告。有什么想法吗
因为虽然每个橘子都是水果,但不是每个水果都是橘子
cellforrowatinexpath:
只知道表中包含UITableViewCell
s(水果)。如果您知道返回的单元格是UserFavoritesCell
(橙色),那么您可以通过强制转换断言:
... cell = (UserFavoritesCell *) ...
如果没有断言(并且编译器相信您能正确地得到它),编译器只能知道它有一个
UITableViewCell
,因此会发出警告。为什么您使用该单元格而不是使用实际的数据源来获取gaugeID?这个方法实际上会从队列中重新创建或调用单元格,只是为了检索数据中已经存在的属性?