我正在尝试实现一个 CollectionViewController,其中图像大小显示正确。但是当我实现方法“collectionViewLayout sizeForItemAtIndexPath”时,没有显示任何图像。有人知道为什么吗?这是我的 CollectionViewController 代码:
#import "PhotosViewController1.h"
@interface PhotosViewController1 ()
@end
@implementation PhotosViewController1
- (void)viewDidLoad
{
[super viewDidLoad];
_carImages = [@[@"Wild-Love.jpg",
@"d919004663f99c934b20c1bd46fcad3e.jpg",
@"164_v-viergalerie.jpg"] mutableCopy];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:
(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return _carImages.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PhotosCell *myCell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"MyCell"
forIndexPath:indexPath];
UIImage *image;
int row = [indexPath row];
image = [UIImage imageNamed:_carImages[row]];
myCell.imageView.image = image;
return myCell;
}
#pragma mark -
#pragma mark UICollectionViewFlowLayoutDelegate
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *image;
int row = [indexPath row];
image = [UIImage imageNamed:_carImages[row]];
return image.size;
}
@end