简体   繁体   English

在自定义UICollectionView中出列时返回了错误的单元格

[英]Wrong cells returned when dequeuing in custom UICollectionView

I'm building an UICollectionView with a custom layout. 我正在构建一个带有自定义布局的UICollectionView。

The layout is 3x4 with horizontal scrolling. 布局为3x4,水平滚动。 I got the layout of the cells and the page scrolling working just fine. 我得到了单元格的布局和页面滚动工作得很好。

My expected result is something like this: 我的预期结果是这样的:

在此输入图像描述

(available at http://www.tinyuploads.com/images/0EVQnT.png ) (见http://www.tinyuploads.com/images/0EVQnT.png

However, when scrolling it seems the wrongs cells are being dequeued, and instead my actual result is this: 但是,当滚动时,似乎错误的单元格正在出列,而我的实际结果是:

在此输入图像描述

(available at http://www.tinyuploads.com/images/8lvJId.png ) (见http://www.tinyuploads.com/images/8lvJId.png

Furthermore, when I scroll back to first page "A" is no longer in the first position. 此外,当我向后滚动到第一页时,“A”不再处于第一个位置。

My datasource and delegate methods looks like this: 我的数据源和委托方法如下所示:

#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);
}

I'm in doubt of as how I should be thinking of sections. 我怀疑我应该如何思考各个部分。 As you see here I have just one section containing all my items (contents of the alphabet). 如您所见,我只有一个部分包含我的所有项目(字母表的内容)。

Any help is highly appreciated. 任何帮助都非常感谢。

WFrom what I can tell it looks like the subclass may not have been implemented correctly. WFrom我可以告诉它看起来子类可能没有正确实现。 Here is an example using the built in Horizontal Flow Layout that you can use to modify your code. 下面是使用内置水平流布局的示例,您可以使用它来修改代码。

Note: This example assumes you have setup the correct size in the storyboard and you will have 3 columns by 4 rows. 注意:此示例假定您在故事板中设置了正确的大小,并且您将有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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM