我按照本教程创建了一个 UICollectionView。我复制了他们的代码没有问题,但是当我尝试它时,它崩溃了
- (void)viewDidLoad
{
[super viewDidLoad];
gridImages = [[NSMutableArray alloc]init];
[gridImages addObject: @"test.jpg"];
[gridImages addObject: @"test.jpg"];
[gridImages addObject: @"test.jpg"];
[gridImages addObject: @"test.jpg"];
[gridImages addObject: @"test.jpg"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return gridImages.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *gridImageView = (UIImageView *)[cell viewWithTag:100];
gridImageView.image = [UIImage imageNamed:[gridImages objectAtIndex:indexPath.row]];
return cell;
}
崩溃在这一行:
UIImageView *gridImageView = (UIImageView *)[cell viewWithTag:100];
如果我将 viewwithtag 更改为 99 或 101,它可以工作,但集合视图只是一个框,而不是 test.jpg 图像。viewwithtag 到底是什么?
碰撞:
[UICollectionViewCell setImage:]: unrecognized selector sent to instance 0x1659b300'
知道发生了什么吗?
编辑:对不起,标签 100 是与故事板中集合视图的 UIImageView 关联的标签。虽然仍然崩溃