【发布时间】:2015-08-30 11:43:41
【问题描述】:
我尝试了非常简单的事情:在按下按钮时在UICollectionViewCell's imageView 上添加图像。 (实际上,CollectionViewCell - 是带有UIImageView IBOutlet 的自定义单元格)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
self.cell=cell;
return cell;
}
- (IBAction)actionAddImage:(UIBarButtonItem *)sender {
UIImage* image=[UIImage imageNamed:@"1.jpg"];
self.cell.imageView.image=image;
NSIndexPath* path= [self.collectionView indexPathForCell:self.cell];
[self.collectionView reloadItemsAtIndexPaths:@[path]];
}
只有一个单元格。在第一个按钮按下没有任何反应,在第二个 - 图像出现。我输入NSLog,并透露,
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
返回新单元格而不是现有单元格。虽然indexPath 很强烈(section=0, row=0),即只有一个细胞。
但是,reloadData 可以完美运行,但当然没有动画。
- (IBAction)actionAddImage:(UIBarButtonItem *)sender {
UIImage* image=[UIImage imageNamed:@"1.jpg"];
self.cell.imageView.image=image;
[self.collectionView reloadData];
}
【问题讨论】:
标签: ios objective-c uicollectionview uicollectionviewcell