1

我正在尝试在背景是书架的 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 个问题时,它看起来不错,但随着越来越多的问题被添加并且您开始必须滚动,它们变得相当不正常:

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

4

1 回答 1

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];
于 2013-07-05T18:08:29.943 回答