我正在水平集合视图中显示从服务器收到的动态图像.当我设置集合视图时,我设置了这个:
-(void)setupCollectionView{ [self setupPageControlView]; self.screensCollectionView.delegate=self; self.screensCollectionView.dataSource=self; [self.screensCollectionView registerNib:[UINib nibWithNibName:@"FGAppScreensCollectionViewItemCell" bundle:nil] forCellWithReuseIdentifier:@"screenCell"]; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; [flowLayout setMinimumInteritemSpacing:0.0f]; [flowLayout setMinimumLineSpacing:0.0f]; [flowLayout setItemSize:CGSizeMake(165,255)]; //[self.screensCollectionView setPagingEnabled:YES]; [self.screensCollectionView setCollectionViewLayout:flowLayout];
}
对于cellForRowAtIndexPath我下载这样的图像:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIdentifier = @"screenCell"; FGAppScreensCollectionViewItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[self.imageURLArray objectAtIndex:indexPath.row]]]; UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; [spinner startAnimating]; [spinner setFrame:CGRectMake(50,100,20,20)]; [cell.contentView addSubview:spinner]; [cell.screenImageView setImageWithURLRequest:urlRequest placeholderImage:nil success:^(NSURLRequest *request,NSHTTPURLResponse *response,UIImage *image) { //STOP THE SPINNER for (UIView* view in [cell.contentView subviews]) { if ([view isKindOfClass:[UIActivityIndicatorView class]]) { [view removeFromSuperview]; } } cell.screenImageView.image = image; } failure:^(NSURLRequest *request,NSError *error) { // }]; [cell.screenImageView setImageWithURL:[NSURL URLWithString:[self.imageURLArray objectAtIndex:indexPath.row]]]; if (self.delegate) { UITapGestureRecognizer* g = [[UITapGestureRecognizer alloc] initWithTarget:self.delegate action:@selector(showPopUpImageView:)]; [cell.screenImageView addGestureRecognizer:g]; } return cell;
}
但是,集合视图中显示纵向和横向图像.肖像图像显示得很好,但在集合视图中景观图像看起来很小.我需要做的是根据图像大小调整单元格大小,以便填充正确的宽高比?
我添加了如下所示的方法,但问题是它在开始时一次调用所有单元格,并且一旦我在cellForRowAtIndexPath方法中下载图像就不会被调用:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(165,255);
}
因此,我想知道如何实现此功能,如果需要更多信息,请告诉我们?