简体   繁体   中英

UICollectionView and core data

Here i am using core data with UICollectionView and here is some problem happen whenever i reload 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).

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] ). 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];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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