我目前正在尝试实现一个简单的两行 UICollectionView。因为我在使用涉及 Core Data 的更复杂的数据源时已经遇到了这个问题,所以我把它变成了一个包含一些字符串的 NSArray。问题还是一样的。
- (NSArray *)collectionViewData
{
return @[@"zero",@"one",@"two",@"three",@"four",@"five",@"six",@"seven",@"eight",@"nine",@"ten",@"eleven",@"twelve",@"thirteen",@"fourteen",@"fifteen",@"sixteen", @"seventeen"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!self.collectionView) {
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(160, 120);
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 10;
self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
self.collectionView.dataSource = self;
[self.collectionView registerClass:[BYCollectionViewCell class] forCellWithReuseIdentifier:@"CELL_ID"];
[self.view addSubview:self.collectionView];
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.collectionViewData count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
BYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL_ID" forIndexPath:indexPath];
cell.title = self.collectionViewData[indexPath.row];
return cell;
}
http://bytolution.com/scrnsht.png
如您所见,单元格的顺序错误。当我将其设置minimumLineSpacing
为 0 时,他们甚至改变了他们的位置。
如何正确实现 UICollectionView?