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

Posted

技术标签:

【中文标题】在自定义 UICollectionView 中出列时返回错误的单元格【英文标题】:Wrong cells returned when dequeuing in custom UICollectionView 【发布时间】:2013-01-06 15:39:32 【问题描述】:

我正在构建一个带有自定义布局的 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);

我怀疑我应该如何考虑部分。正如您在此处看到的,我只有一个部分包含我的所有项目(字母表的内容)。

非常感谢任何帮助。

【问题讨论】:

你能发布你的自定义布局代码吗? 我也对你的问题感兴趣。你是 UICollectionViewFlowLayout 的子类吗?您使用的是 Autolayout 还是旧的 spring 和 struts?我发现不使用这两个意味着事情有效,但当然需要这些。此外,您正在使用自己的 UICollectionViewCell 类。放入这个方法(void)prepareForReuse;在您的单元格类中,您可以检查即将被重用的单元格。这可能会让您更深入地了解事物。 这是调用滚动问题,参考:***.com/questions/14287353/… 我建议将其设为 n=round(items/12) 部分,以便轻松完成并正确处理工作流程。根据您的要求,您不需要子类化自定义布局,您只需继续使用 UICollectionViewFlowLayout。 @James Fox 如果您遇到同样的困难,请提供解决方案。 【参考方案1】:

W据我所知,子类可能没有正确实现。这是一个使用内置水平流布局的示例,您可以使用它来修改代码。

注意:此示例假定您已在情节提要中设置了正确的大小,并且您将拥有 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

【讨论】:

以上是关于在自定义 UICollectionView 中出列时返回错误的单元格的主要内容,如果未能解决你的问题,请参考以下文章

无法将 UICollectionView 的单元格所需的视图出列

我在自定义 TableViewCell 中有一个 UICollectionView,但我无法自动调整 CollectionView 的大小

如何在自定义 UICollectionView 的旋转后触发 drawrect?

在 UIcollectionview 中使多个单元格出列的方法

出列后自定义 UICollectionViewCell 属性为空

UICollectionView 自定义单元格未重新加载