UICollectionView无需重用单元格

vuu*_*duu 11 cocoa-touch objective-c ios

只是好奇,是否可以禁用重用功能UICollectionview?我的细胞数量有限,可能会有所不同,但细胞的重新初始化可能有点重,我最好不要重复使用它们.尝试初始化单元格而没有dequeueReusableCellWithReuseIdentifier得到异常:

NSInternalInconsistencyException '理由是:' 从返回的CollectionView的观点:cellForItemAtIndexPath:forIndexPath:未通过调用-dequeueReusableCellWithReuseIdentifier检索.

Cal*_*leb 6

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

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

尝试在没有dequeueReusableCellWithReuseIdentifier的情况下初始化单元格我得到了异常:

我认为这是一个强有力的迹象,表明你的问题的答案是否定的.此外,文件说:

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

所以,,.


Ale*_*lla 6

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

- (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];

    // ...
    // ...
    // ...
}
Run Code Online (Sandbox Code Playgroud)

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

- (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++;

    // ...
    // ...
    // ...
}
Run Code Online (Sandbox Code Playgroud)

重要提示:我只是在这里回答这个问题,完全不推荐,特别是第二种方法.我在第一种方法中使用了第一种方法,我有多方向滚动,并且有一些关于我需要为每个单元格调用的关注tvOS的委托方法.