[英]UICollectionView and core data
Here i am using core data with UICollectionView and here is some problem happen whenever i reload CollectionView. 在这里,我使用UICollectionView的核心数据,每当我重新加载CollectionView时,都会发生一些问题。
The problem is that when i create an album and set on it a latest photo(if any photo is exist other wise no setting for image on album) which is getting from entity "PHOTO" then reload collection view then this album also set an image on its self (an album is actually a CollectionViewCell). 问题是,当我创建相册并在其上设置从实体“ PHOTO”获取的最新照片(如果存在其他照片时,否则在相册上没有图像的设置)然后重新加载收藏夹视图,则此相册也设置了图像本身(相册实际上是CollectionViewCell)。
Its really irritating. 真的很烦人。 If any one can solve this Problem then kindly guide me.
如果任何人都可以解决此问题,请指导我。
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor yellowColor];
//Get Album Names
GMSAlbum *objAlbumEntity = [arrayAlbums objectAtIndex:indexPath.row];
NSLog(@"AlbumName = %@",objAlbumEntity.name);
delegate->currentAlbum = [arrayAlbums objectAtIndex:indexPath.row];
//NSLog(@"current path = %@",delegate->currentAlbum);
//Get Last image
NSString *imagePath = [self getLastImage:delegate->currentAlbum];
NSLog(@"Image Path = %@",imagePath);
if (![imagePath isEqualToString:@""]) {
UIImageView *imageThumb = [[UIImageView alloc] initWithFrame:
CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
[imageThumb setImage:[UIImage imageWithContentsOfFile:imagePath]];
[cell addSubview:imageThumb];
}
return cell;
}
-(NSString *)getLastImage:(Album *)album{
NSString *imagePath = @"";
GMSPhotoModel *objPhotoModel = [[GMSPhotoModel alloc] init];
NSMutableArray *arryPhotos = [objPhotoModel getAllPhotoForAlbum:album];
NSLog(@"Array count = %d",[arryPhotos count]);
if ([arryPhotos count]) {
GMSPhoto *objPhotoName = [arryPhotos lastObject];
imagePath = objPhotoName.path;
NSLog(@"Naming Album = %@",objPhotoName.name);
}
NSLog(@"Path = %@",imagePath);
return imagePath;
}
Your UICollectionViewCell is reused ( [cv dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]
). 您的UICollectionViewCell被重用(
[cv dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]
)。 Thats why it show wrong album picture (Actually, its image from last album). 这就是为什么它显示错误的专辑图片(实际上是上一张专辑的图像)的原因。 To avoid this try clear image if there no image path for current album:
为避免这种情况,如果当前相册没有图像路径,请尝试清除图像:
if (![imagePath isEqualToString:@""]) {
UIImageView *imageThumb = [[UIImageView alloc] initWithFrame:
CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
[imageThumb setImage:[UIImage imageWithContentsOfFile:imagePath]];
[cell addSubview:imageThumb];
}
else
{
[imageThumb setImage:nil];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.