我可以避免重用一些UICollectionViewCells吗?

时间:2016-05-10 07:14:40

标签: ios uicollectionview

通常我们重用UICollectionViewCells。我们

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SomeCell* cell =  [cv dequeueReusableCellWithReuseIdentifier:kCellId forIndexPath:indexPath];
    [cell configureWithData:someData];
    return cell;
}

但有时配置一个特定的单元是耗时的(例如,当它应该删除所有以前的子视图并创建新的子视图时),并且它只需要配置一次。我只想调用方法configureWithData一次,而不是每当用户将该单元格滚入和移出屏幕时。

使用UITableView时,我会将该特定单元格存储为属性。但我发现我不能用UICollectionView做同样的事情。当我这样做时:

- (void)viewDidLoad {
    [self.collectionView registerClass:SomeCell.class forCellWithReuseIdentifier:kCellId];
    self.specificCell = [self.collectionView dequeueReusableCellWithReuseIdentifier:kCellId forIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
    [self.specificCell configureWithData:someData];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    return self.specificCell;
}

该单元的位置远非正确,它飞向其他部分。

所以我想知道如何避免重复使用UICollectionViewCell,并且总是为一个特定的部分使用一个特定的单元格?如果我不能这样做,那么每当用户将该单元格滚入和移出屏幕时,我是否可以避免调用configureWithData

提前致谢!

0 个答案:

没有答案