Property 'label' not found on object of type

1,这是一个collection view的简单程序。每个cell包含一个label显示一个1-200的数字
2,定义了一个CollectionViewCell类,一个继承它的GridCell类。
3,storyboard中,cell的类为GridCell,identifier为GridCell
4, label是GridCell的属性,用来显示数字
CollectionViewController的以下方法中,总是报错,找不到celllabel属性:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GridCell" forIndexPath:indexPath];

    NSNumber *number = numbers[indexPath.row];

    cell.label.text= [number description]; //这句报错,Property 'label' not found on object of type'UICollectionViewCell'

    return cell;
}
阅读 6.8k
2 个回答
 UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GridCell" forIndexPath:indexPath];

问题在这一句,你用父类声明的,怎么能调用到子类的属性
按你描述,应该用GridCell声明

原因是 cell应该是GridCell,放在这里供大家参考吧。

有没有提供ios开发这种实习的岗位的?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题