所以我遇到了一个独特的问题。我有一个 UICollectionView 可以找到,只要我从中提取的数组中至少有 2 个对象。
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSLog(@"Allocating space for %i photos in collectionView", [CCPhotos sharedPhotos].photos.count);
return [CCPhotos sharedPhotos].photos.count;
}
// configure cells
- (UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"PhotoCell" forIndexPath: indexPath];
NSLog(@"Creating cell for row %i", indexPath.row);
for (UIView* view in cell.contentView.subviews)
[view removeFromSuperview];
cell.layer.cornerRadius = 20.0f;
cell.layer.masksToBounds = YES;
// Add image to cell
UIImageView* imageView = [[UIImageView alloc] initWithImage: [[CCPhotos sharedPhotos].photos objectAtIndex: indexPath.row]];
// size imageView and set content mode
imageView.frame = cell.contentView.frame;
// add imageView to cell
[cell.contentView addSubview: imageView];
return cell;
}
numberOfItemsInSection
总是被调用并返回正确的值,但如果它小于 2,cellForItemAtIndexPath
则不会被调用。有任何想法吗?
编辑:我忘了提到我使用了自己的流布局来设置它:
- (id) init
{
if (self = [super init])
{
self.itemSize = CGSizeMake(ITEM_WIDTH, ITEM_HEIGHT);
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.sectionInset = UIEdgeInsetsMake(0, (320 - ITEM_WIDTH) / 2, 0, 0);
self.minimumLineSpacing = 320 - ITEM_WIDTH;
}
return self;
}