朋友在这里我使用UICollectionview来显示图像,但我的图像来自Web服务。 现在的问题是我从网上获得了所有数据,但我如何在UICollectionview中插入项目。 我知道insertItemAtIndexPath方法,但不知道在哪里以及如何使用它。 请帮帮我。 提前谢谢。
答案 0 :(得分:1)
您可以使用委托模式下载数据。
委托将阻止实现下载完成协议的类,以便您可以通过[YouCollectionView reloadData]
调用的方法加载数据
并且为了显示你的图像,你的collectionView可以基于一个数组,例如,委托可以发送一个包含你的图像/网址的数组。
/*! This delegate method is called when the items have been downloaded. They're then stored into an array.*/
- (void)didDownloadItems:(NSMutableArray*)responseArray{
_imgArray = responseArray;
[yourCollectionView reloadData];
}
然后在你的(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UICollectionViewCell *cell = [yourCollectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
// Retrieve the img
UIImage *img = [_imgArray objectAtIndex:indexPath.row];
//do your stuff with the img
return cell;
}