カスタムレイアウトでUICollectionViewを構築しています。
レイアウトは3x4で、水平スクロールがあります。セルのレイアウトとページのスクロールがうまく機能しました。
私の期待する結果は次のようなものです。
(http://www.tinyuploads.com/images/0EVQnT.pngで入手可能)
ただし、スクロールすると、間違ったセルがデキューされているように見えます。代わりに、実際の結果は次のようになります。
(http://www.tinyuploads.com/images/8lvJId.pngで入手可能)
さらに、最初のページに戻ると、「A」は最初の位置にありません。
私のデータソースとデリゲートメソッドは次のようになります。
#pragma mark - UIViewController Life Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.collectionView.backgroundColor = [UIColor colorWithWhite:0.25f alpha:1.0f];
self.alphabet = [NSMutableArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"X", @"Y", @"Z", nil];
self.colors = [NSMutableArray arrayWithObjects:[UIColor colorWithRed:(135/255.0) green:(175/255.0) blue:(88/255.0) alpha:1], [UIColor colorWithRed:(65/255.0) green:(124/255.0) blue:(185/255.0) alpha:1], [UIColor colorWithRed:(201/255.0) green:(189/255.0) blue:(64/255.0) alpha:1], nil];
[self.collectionView reloadData];
}
#pragma mark - UICollectionView datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.alphabet.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *title = [self.alphabet objectAtIndex:indexPath.row];
UIColor *backgroundColor = [self.colors objectAtIndex:indexPath.row % 3];
CategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CategoryCellIdentifier forIndexPath:indexPath];
cell.title.text = title;
cell.backgroundColor = backgroundColor;
[cell setNeedsLayout];
return cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"You touched: %d", indexPath.row);
}
私はセクションをどのように考えるべきか疑問に思っています。ここに表示されているように、すべてのアイテム(アルファベットの内容)を含むセクションが1つだけあります。
どんな助けでも大歓迎です。