This one很容易放入拉刷新。
對於圖像加載,我寫了下面類別的UIImageView:
// .h
@interface UIImageView (UIImageView_Load)
- (void)loadFrom:(NSURL *)url completion:(void (^)(UIImage *))completion;
@end
// .m
#import "UIImageView+Load.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIImageView (UIImageView_Load)
- (void)loadFrom:(NSURL *)url completion:(void (^)(UIImage *))completion {
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (data) {
self.image = [UIImage imageWithData:data];
if (completion) completion(self.image);
}
}];
}
@end
// To use it when building a cell
//...
MyModelObject *myModelObject = [self.myModel objectAtIndex:indexPath.row];
if (myModelObject.image) {
cell.imageView.image = myModelObject.image;
} else {
NSURL *url = [NSURL urlWithString:myModelObject.imageUrl];
[cell.imageView loadFrom:url completion:^(UIImage *image) {
// cache it in the model
myModelObject.image = image;
cell.imageView.image = image;
}];
}
// ...
感謝您的回答@danh!不幸的是,它不包含任何緩存解決方案。但是我會給lea的「拉來刷新」一試。 – Danik 2012-03-28 11:14:43
@Danik - 當然。我粘貼的代碼在回調模型中進行了延遲加載和緩存。 – danh 2012-03-28 13:34:11
是的,我現在看到,您將其緩存在myModelObject.image中!您是否清除了didReceiveMemoryWarning上的緩存或限制它的最大尺寸? – Danik 2012-03-28 18:30:33