UICollectionView不会重新加载数据

时间:2013-12-26 14:39:13

标签: ios objective-c uicollectionview

我的应用中的集合视图会在第一时间正确重新加载数据。下次我希望集合视图重新加载数据(我在调用reloadData:方法之前将数据源设置为nil),但集合视图单元格仍然包含旧数据。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionCellId forIndexPath:indexPath] ;
    if (!cell) {
        cell = [[UICollectionViewCell alloc] init] ;
    }

    [self setupCellImage:cell indexPath:indexPath] ;

    return cell ;
}

-(void) setupCellImage:(UICollectionViewCell *) cell indexPath:(NSIndexPath *) indexPath{
    cell.backgroundColor = [UIColor whiteColor] ;

    NSDictionary *mediaObject = popObjects[indexPath.row] ;
    NSLog(@"index.row:%d",indexPath.row) ;

    UIImageView *imageView = [[UIImageView alloc] init] ;
    [imageView clipsToBounds] ;
    imageView.frame = cell.contentView.bounds ;

    UIImage *placeholderImage = [UIImage imageNamed:@"black"] ;

    NSURL *imgUrl = [NSURL URLWithString:mediaObject[@"images"][@"low_resolution"][@"url"]];
    cell.backgroundColor = [UIColor blackColor]  ;

    [cell.contentView addSubview:imageView   ] ;

    if (![[isPhotoLoadedDic valueForKey:[NSString stringWithFormat:@"%@",indexPath]] boolValue]) {
        [imageView setImageWithURL:imgUrl placeholderImage:placeholderImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
            // set the isPhotoLoadedDic
            if (image && !error) {
                [isPhotoLoadedDic setValue:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"%@",indexPath]] ;
                NSLog(@"row:%d loaded",indexPath.row)     ;
            } else{
                [isPhotoLoadedDic setValue:[NSNumber numberWithBool:NO] forKey:[NSString stringWithFormat:@"%@",indexPath]] ;
                NSLog(@"row:%d  fail to load this image",indexPath.row) ;
            }
        }]  ;
    }
}

requestLoading:request是我将数据源设置为nil

的方法
/**
 * Called just before the request is sent to the server.
 */
- (void)requestLoading:(IGRequest *)request {
    //reset objects before sending request
    isPhotoLoadedDic = [NSMutableDictionary dictionary ] ;
    popObjects = nil ;
    [self.collectionView reloadData] ;
}

任何人都可以帮助我吗?请。

0 个答案:

没有答案