正如您所提到的,NScollectionView
一次构建所有单元格。特别是如果在单元内有很多图像会导致性能问题。这是我解决问题的方法。可能有更好的解决方案,但只要这对我来说工作得很好。 很长时间以来,我一直在用Instruments观察代码的性能,并没有发现任何缺陷。
- (void)windowDidLoad {
//Here adding observer to NSCollectionView's scrollview to monitor bounds changes.
NSView *contentView = [scrollViewOfYourCollectionView contentView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boundDidChange:) name:NSViewBoundsDidChangeNotification object:contentView];
}
- (void)boundDidChange:(NSNotification *)notification
{
NSRect collectionViewVisibleRect = yourCollectionView.visibleRect;
//collectionViewVisibleRect.size.height += 300; //If you want some preloading for lower cells...
for (int i = 0; i < yourDataSourceArray.count; i++) {
NSCollectionItem *item = [yourCollectionView itemAtIndex:i];
if (NSPointInRect(NSMakePoint(item.view.frame.origin.x, item.view.frame.origin.y), collectionViewVisibleRect) == YES)
{
if (item.imageView.image == nil) {
dispatch_async(dispatch_get_main_queue(), ^{
//Fill your cells.
//to showing/downloading images I am using SDWebImage.
});
}
}
}
}
谢谢,明天我会试试并报告回来!你觉得你需要做特别的事情来清理那些看不见的细胞吗? – user441669
这是我原来的计划,但是一旦我看到它不影响我离开它的表现。如果你想清理它们,你可以将imageView图像设置为零,如果单元格不在可见矩形中。 – mohacs
非常适合我,谢谢。 – user441669