【问题标题】:UICollectionView and core dataUICollectionView 和核心数据
【发布时间】:2014-02-14 06:42:01
【问题描述】:

这里我将核心数据与 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;
}

【问题讨论】:

  • 说出你的要求……你不清楚……
  • 为什么在重新加载数据时相册设置了图片而相册中没有照片
  • 如果没有图像,您在 iPad 上查看/编辑 MS Word 文档会得到什么?
  • 当我与相关相册合影时,没有任何图像路径是空的,但似乎每次都会发生单元格笔记上的图像但是发生了:(
  • 是否进入 if 条件?

标签: ios objective-c core-data uicollectionview


【解决方案1】:

您的 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];

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 2010-12-04
    • 2014-09-18
    • 2013-01-24
    • 2011-12-04
    • 2011-10-22
    • 1970-01-01
    相关资源
    最近更新 更多