我正在尝试在UICollectionView中布置杂志封面的集合,其中背景是书架。我希望它每个架子有2个杂志,其余的在向下滚动时可以看到。这是我的代码:
-(void)viewDidLoad {
UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];
self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"shelves.png"]];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
NSLog(@"1");
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [_allEntries count];
NSLog(@"2");
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
static NSString *cellIdentifier = @"cvCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
UIImageView *titleLabel = (UIImageView *)[cell viewWithTag:100];
UILabel *titleLabel2 = (UILabel *)[cell viewWithTag:200];
NSString *thearticleImage = entry.articleImage;
[titleLabel setImageWithURL:[NSURL URLWithString:entry.articleImage] placeholderImage:[UIImage imageNamed:@"icon@2x.png"]];
// [titleLabel2 setText:entry.articleTitle];
return cell;
}
我正在使用XIB创建collectionView单元格。
当只有2个问题时,它看起来很好,但随着越来越多的问题被添加并且您开始必须滚动,它们会完全关闭:
答案 0 :(得分:0)
您在单元格之间使用填充计算UICollectionViewCell可能是错误的。您可以播放图像大小或以编程方式设置单元格大小:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(192.f, 192.f);
}
您还可以在Interface Builder中使用填充。
您也可以通过编程方式执行此操作:
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(100, 200)];
[flowLayout setMinimumInteritemSpacing:10];
[flowLayout setMinimumLineSpacing:10];