[英]Layout of Images in UICollectionView
I am trying to layout a collection of Magazine covers in a UICollectionView in which the background is a bookshelf. 我试图在背景为书架的UICollectionView中布局杂志封面的集合。 I want it to have 2 magazines per shelf, and the rest to be seen when scrolling down. 我希望它的每个架子上有2个杂志,其余的当向下滚动时可以看到。 Here is my code I have: 这是我的代码:
-(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;
}
I am using an XIB to create the collectionView cell. 我正在使用XIB创建collectionView单元。
When there are only 2 issues, it looks fine, but as more and more issues get added and you begin to have to scroll, they get quite off: 当只有2个问题时,它看起来不错,但是随着越来越多的问题被添加并且您不得不开始滚动,它们变得相当成功:
Your calculation of your UICollectionViewCell with paddings between cells could be kind a wrong. 您对UICollectionViewCell的计算带有单元格之间的填充可能是错误的。 You can either play with size of images or set the size of cell programmatically: 您可以使用图像大小播放,也可以通过编程设置单元格大小:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(192.f, 192.f);
}
You can also play with paddings in Interface Builder. 您也可以在Interface Builder中使用填充。
You can also do this programmatically: 您还可以通过编程方式执行此操作:
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(100, 200)];
[flowLayout setMinimumInteritemSpacing:10];
[flowLayout setMinimumLineSpacing:10];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.