[英]Wrong cells returned when dequeuing in custom UICollectionView
我正在構建一個帶有自定義布局的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);
}
我懷疑我應該如何思考各個部分。 如您所見,我只有一個部分包含我的所有項目(字母表的內容)。
任何幫助都非常感謝。
WFrom我可以告訴它看起來子類可能沒有正確實現。 下面是使用內置水平流布局的示例,您可以使用它來修改代碼。
注意:此示例假定您在故事板中設置了正確的大小,並且您將有3列乘4行。
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray* alphabet;
@property (nonatomic, strong) NSMutableArray* colors;
@end
@implementation ViewController
- (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", @"W", @"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{
// Full pages
NSInteger count = self.alphabet.count /12 + (self.alphabet.count % 12 > 0 ? 1 : 0);
count *= 12;
return count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSInteger row = indexPath.row % 4;
NSInteger col = indexPath.row / 4;
NSInteger page = col /3 * 12;
col = col % 3;
NSInteger ii = row*3+col + page;
NSString *title = ii >= self.alphabet.count ? nil : [self.alphabet objectAtIndex:ii];
UIColor *backgroundColor = [self.colors objectAtIndex:col];
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UILabel* label = (UILabel*)[cell viewWithTag:1]; // label added in storyboard
label.text = title;
cell.backgroundColor = backgroundColor;
[cell setNeedsLayout];
cell.hidden = title == nil;
return cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"You touched: %d", indexPath.row);
}
@end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.