DEV Community

lichuange
lichuange

Posted on

Testing UICollectionViewCell reuse behavior

The behavior of reusing UICollectionViewCell while reload UICollectionView is diverse.

Here gives some cases to test the result.

Case1: Call the method reloadData directly

eg.

[self.collectionView reloadData];
Enter fullscreen mode Exit fullscreen mode

Result:
All cells are recycled and reused in an unordered manner, without any animation.

Case2: Call the method reloadData inside performBatchUpdates

eg.

[self.collectionViewperformBatchUpdates:^{
   [self.collectionView reloadData];
} completion:^(BOOL finished) {
}];
Enter fullscreen mode Exit fullscreen mode

Result:
The method cellForItemAtIndexPath is not invoked, and no cells are recycled or reused, but with animation.

Case3: Call the method reloadItemsAtIndexPaths directly

eg.

[self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:btn.tag inSection:0]]];
Enter fullscreen mode Exit fullscreen mode

Result:
The method cellForItemAtIndexPath is called for the current row, where a new cell is created and applied, while the old one is recycled to reuse pool, with animation.

Case4: Call the method reloadItemsAtIndexPaths inside performBatchUpdates

eg.

[self.collectionViewperformBatchUpdates:^{
    [self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPathindexPathForRow:btn.taginSection:0]]];
} completion:^(BOOL finished) {
}];
Enter fullscreen mode Exit fullscreen mode

Result:
Same as Case3, the method cellForItemAtIndexPath is called for the current row, where a new cell is created and applied, while the old one is recycled to reuse pool, with animation.

Top comments (0)