Tag: 懒惰加载

使用SDWebImageManager在UICollectionView中延迟加载图像

在我的方法cellForItemAtIndexPath我有这样的代码: SDWebImageManager *manager = [SDWebImageManager sharedManager]; [manager downloadWithURL:[NSURL URLWithString:coverURL] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { RRRBigItemCell *cell = (RRRBigItemCell *)[collectionView cellForItemAtIndexPath:indexPath]; if (cell) { RRRImageView *coverView = (RRRImageView *)[cell viewWithTag:COVER_TAG]; coverView.image = image; } }]; 当我启动应用程序,我向下滚动然后一切都OK。 但是当我滚动比已经显示的单元格没有加载图像(单元格==零)。 如果我再次向下滚动问题仍然是显示单元格,只有新的加载图像。 任何想法我做错了什么?

懒惰实例化缺点iOS

所以似乎懒惰的实例化被广泛使用,每个人都知道懒惰实例的优点。 这导致了一个问题:我们应该懒惰实例化每个对象? 我严重怀疑。 所以问题是,懒惰实例有什么缺点? 取自(苹果示例LocateMe)的样本: – (NSDateFormatter *)dateFormatter { if (dateFormatter == nil) { dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; [dateFormatter setTimeStyle:NSDateFormatterLongStyle]; } return dateFormatter; } 这将给我们的优势,只有在需要的时候初始化这个对象。 顺便说一下,从苹果上面的例子,似乎他们只懒得实例化“只读”的对象。