繁体   English   中英

UICollectionView和核心数据

[英]UICollectionView and core data

在这里,我使用UICollectionView的核心数据,每当我重新加载CollectionView时,都会发生一些问题。

问题是,当我创建相册并在其上设置从实体“ PHOTO”获取的最新照片(如果存在其他照片时,否则在相册上没有图像的设置)然后重新加载收藏夹视图,则此相册也设置了图像本身(相册实际上是CollectionViewCell)。

真的很烦人。 如果任何人都可以解决此问题,请指导我。

- (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;
}

您的UICollectionViewCell被重用( [cv dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath] )。 这就是为什么它显示错误的专辑图片(实际上是上一张专辑的图像)的原因。 为避免这种情况,如果当前相册没有图像路径,请尝试清除图像:

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM