2016-03-08 73 views
1

我收到一个错误,我不明白。我继承UICollectionViewCell如下:Objective C子类iVar不采用相同的指针类型?

CollectionViewCell.h

@interface CollectionViewCell : UICollectionViewCell 
@property (weak, nonatomic) IBOutlet UILabel *txtLabel; 

在一个视图控制器,我用它如下:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 

    CollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; 
    UICollectionViewCell *cellOne = [collectionView cellForItemAtIndexPath:indexPath]; 

使用CollectionViewCell第一个呼叫发出警告,第二届一个不,他们都是同一班。当它被选中时更改单元格并访问它中的数据。

警告与CollectionViewCell的行是“不兼容的指针类型初始化‘CollectionViewCell *’与类型的表达式‘UICollectionViewCell *’

如果它们都相同的类,唯一的区别是IVAR txtLabel,他们为什么会以不同的方式工作?

我想获得使用indexPath的单元格。我假设有一些存储这些单元格,您可以使用传入的indexPath访问这些单元格。

Q1。我正确地假设我可以通过使用传入的indexPath来获取单元格吗? Q2302。为什么一个电话会工作而另一个不是同一个班级?

回答

1
CollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; 

方法-cellForItemAtIndexPath:被定义为返回UICollectionViewCell。编译器不知道实际返回的是CollectionViewCell

你必须垂头丧气告诉编译器,你知道你在做

CollectionViewCell *cell = (CollectionViewCell *) [collectionView cellForItemAtIndexPath:indexPath]; 

这是什么会删除该警告。这只是一个警告。它警告你可能有错误,但代码会正常工作。