UICollectionView从API逐个加载单元格

时间:2015-07-03 10:36:31

标签: ios objective-c uicollectionview cells

我正在使用Flickr的API来检索图像

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    photoURLString =
    [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_m.jpg",
     [photo objectForKey:@"farm"], [photo objectForKey:@"server"],
     [photo objectForKey:@"id"], [photo objectForKey:@"secret"]];

    [self.photoURLsLargeImage addObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoURLString]]]];
    [self.flickrCollectionView reloadData];

    NSLog(@"photoURLsLareImage: %@\n\n", photoURLString);
    [self.flickrCollectionView reloadItemsAtIndexPaths:[self.flickrCollectionView indexPathsForVisibleItems]];

而不是等待他们全部加载,最后做

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellIdentifier = @"CustomCell";

    // cast uicollectionviewcell into instance of customcell
    CustomCell *cell = (CustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];


    [cell.titleLabel setImage:[self.photoSmallImageData objectAtIndex:indexPath.row]];


    return cell;
}

我想在从Flickr的API中检索后立即逐个加载单元格。我想我必须在我的连接方法中这样做,但我无法从那里访问我的单元格。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

直接从连接方法访问单元格不是一个好习惯。
理想情况下,设计应该是“控制器< - > FlickrManager< - > HTTPManager”所有连接代码都应该驻留在HTTPManager中。
FlickrManager应通知控制器有关新图像添加的信息。