5

現在、単純な 2 行の 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 を正しく実装するにはどうすればよいですか?

4

0 に答える 0