【问题标题】:UICollectionView with dynamic Cell Identifier具有动态单元标识符的 UICollectionView
【发布时间】:2015-01-07 14:00:12
【问题描述】:

我希望 UICollection 视图具有动态 CellIdentifier 像下面这样。

    NSString *strIdentifier = [NSString stringWithFormat:@"cellIdentifier%d",indexPath.row];
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:strIdentifier forIndexPath:indexPath];

我该怎么做?如果可能请帮忙!!! 谢谢

编辑

我已使用此代码注册了我的所有标识符

    //CollectionView
    self.mpCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width,  frame.size.height) collectionViewLayout:layout];
    [self.mpCollectionView setDataSource:self];
    [self.mpCollectionView setDelegate:self];
    for(int i=0;i<arrayExplorerItems.count;i++)
    {
        NSString* strIdentifier = [NSString stringWithFormat:@"cellIdentifier%d",i];
        NSLog(@"registered Id:%@",strIdentifier);
        [self.mpCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:strIdentifier];
    }

 and my cellForItemAtIndeaxPath is 

    UICollectionViewCell *cell;
    NSString *strIdentifier = [NSString stringWithFormat:@"cellIdentifier%d",indexPath.row];
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:strIdentifier forIndexPath:indexPath];

但是给了我这个错误

无法使同类视图出队:带有标识符 cellIdentifier0 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或类或连接情节提要中的原型单元格

【问题讨论】:

  • 您在哪里/何时执行注册代码循环?
  • 分配collectionview后不久
  • 我是在调用 cellForItem 之前注册 NSLog 吗?
  • @rounak 我没有收到日志
  • 那么你的代码没有被执行。您的控制流/逻辑有问题,而不是 UICollectionView 的问题

标签: ios uicollectionview


【解决方案1】:

这是可能的。您需要先注册所有单元格标识符。

[collectionView registerNib:forCellWithReuseIdentifier:]

[collectionView registerClass:forCellWithReuseIdentifier:]

然后您就可以轻松创建和出列所有准备好的标识符。

【讨论】:

  • 如何注册不同的标识符?
猜你喜欢
  • 2021-02-12
  • 2020-03-08
  • 2020-02-02
  • 1970-01-01
  • 2014-09-04
  • 2020-04-29
  • 1970-01-01
  • 2016-12-22
  • 2014-07-08
相关资源
最近更新 更多