Ios UICollectionView,无需重复使用单元格

Ios UICollectionView,无需重复使用单元格,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,好奇的是,是否可以禁用UICollectionview上的重用功能? 我的单元格数量有限,可能会有所不同,但单元格的重新初始化可能有点重,最好不要重用它们。 尝试在没有dequeueReusableCellWithReuseIdentifier的情况下初始化单元格时出现异常: NSInternalInconsistencyException',reason:'调用-dequeueReusableCellWithReuseIdentifier:forIndexPath未检索从collectionV

好奇的是,是否可以禁用
UICollectionview
上的重用功能? 我的单元格数量有限,可能会有所不同,但单元格的重新初始化可能有点重,最好不要重用它们。 尝试在没有
dequeueReusableCellWithReuseIdentifier
的情况下初始化单元格时出现异常:

NSInternalInconsistencyException',reason:'调用-dequeueReusableCellWithReuseIdentifier:forIndexPath未检索从collectionView:cellForItemAtIndexPath:返回的视图

细胞的重新初始化可能有点重

重置单元格的内容不太可能比创建新单元格更昂贵——单元格重用的全部目的是通过避免不断创建新单元格来提高性能

尝试在没有dequeueReusableCellWithReuseIdentifier的情况下初始化单元格时,出现异常:

我认为这有力地表明你的问题的答案是否定的。进一步说:

…集合视图要求始终将视图出列,而不是在代码中显式创建视图


因此,同样地,no

要禁用单元格重用,只需使用该单元格索引路径的特定标识符将您的单元格出列,然后注册该标识符即可

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = [NSString stringWithFormat:@"Identifier_%d-%d-%d", (int)indexPath.section, (int)indexPath.row, (int)indexPath.item];
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier];

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    // ...
    // ...
    // ...
}
请注意,在上述方法中,重用并不是完全禁用的,因为每个单元格都有一个标识符,这可能是每个人都需要的,但如果需要完全禁用单元格重用性,可以执行以下操作:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static int counter = 0;

    NSString *identifier = [NSString stringWithFormat:@"Identifier_%d", counter];
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier];

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    counter++;

    // ...
    // ...
    // ...
}

重要的:我只是在回答这个问题,这完全是不推荐的,特别是第二种方法。

比不推荐更糟糕;无法注销所有这些标识符,因此您的应用程序可能会很快崩溃。:-)