简体   繁体   English

UICollectionView和核心数据

[英]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.

相关问题 从核心数据加载UICollectionView - load UICollectionView from core data 动画之前在UICollectionView中使用Core Data加载数据 - Load data with Core Data in UICollectionView before animation 核心数据对象更改时更新uicollectionview - Updating uicollectionview when core data object changes 使用Swift自动重新排序核心数据中的UICollectionView - Automatically Reordering UICollectionView in Core Data with Swift 从UICollectionView中选择后,使用核心数据填充详细信息视图 - Use core data to populate detail view after selection from UICollectionView 当添加项目(核心数据)时,更新UICollectionView(卡在insertItemsAtIndexPaths上) - Updating a UICollectionView when an item is added (Core Data), stuck on insertItemsAtIndexPaths 将来自Core Data的调整大小的图像插入UICollectionView的最简单方法是什么? - What is the simplest way to insert resized images from Core Data into a UICollectionView? 由UICollectionView调用以显示单元格时,数据未从Core Data完全加载 - Data not fully loaded from Core Data when being called by UICollectionView to display cell 在将新数据插入核心数据存储区时,如何使UICollectionView始终位于底部 - How to keep UICollectionView always at the bottom while new data is inserted into core data store UICollectionView滚动滞后于核心图 - UICollectionView scroll lags with core plot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM