【发布时间】:2014-12-22 12:47:34
【问题描述】:
我有 2 个集合视图。对于第一个集合视图,我每次只看到一个单元格,但对于第二个集合视图,我每次看到 8 个单元格。
所以对于第二个集合视图,一切正常,但第一个集合视图重复了一些单元格。
如果我在下面使用此代码,则会导致重复单元格出现一些问题。我不确定单元格是否重复,但我的每个单元格都包含显示图像的图像视图。因此,这些单元格中的图像是 100% 保证重复的。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
GalleryCollectionViewCell *collectionViewCell = (GalleryCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"GalleryCollectionViewCell" forIndexPath:indexPath];
NSDictionary *artwork = [self.artworks objectAtIndex:indexPath.item];
[collectionViewCell loadImageWithURLString:artwork[@"image_url"]];
return collectionViewCell;
}
我查看了上面的回调,并为我的第一个集合视图添加了实例,该集合视图也是我的超级视图层次结构中的顶级集合视图。现在源代码如下所示:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if ([collectionView isEqual:self.collectionViewTop])
{
GalleryCollectionViewCell *collectionViewCell = (GalleryCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"GalleryCollectionViewCell" forIndexPath:indexPath];
NSDictionary *artwork = [self.artworks objectAtIndex:indexPath.item];
[collectionViewCell loadImageWithURLString:artwork[@"image_url"]];
return collectionViewCell;
}
else
{
GalleryCollectionViewCell *collectionViewCell = (GalleryCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"GalleryCollectionViewCell" forIndexPath:indexPath];
NSDictionary *artwork = [self.artworks objectAtIndex:indexPath.item];
[collectionViewCell loadImageWithURLString:artwork[@"image_url"]];
return collectionViewCell;
}
}
我不确定为什么需要检查集合视图,但它现在可以工作。每个单元格显示正确的内容。所以第一个集合视图没有重复的问题。
另外,我可以添加代码如何设置图像:
- (void)loadImageWithURLString:(NSString *)urlString
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[self.theImageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"home_screen_logo"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[self.theImageView setImage:image];
[self.indicator stopAnimating];
[self.indicator setHidden:YES];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
}];
}
但我不知道为什么我需要在回调中检查集合视图,因为实际上它与第一个集合视图和第二个集合视图的作用相同。对于加载图像,我使用AFNetworking 功能。
【问题讨论】:
标签: ios uicollectionview uicollectionviewcell afnetworking-2