没有优化前
#import"ViewController.h"
#import"App.h"
@interfaceViewController()
@property(nonatomic,strong)NSArray*apps;
@end
@implementationViewController
- (NSArray*)apps{
if(_apps==nil) {
NSArray*ary = [NSArrayarrayWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"apps"ofType:@"plist"]];
NSMutableArray*ary1 = [NSMutableArrayarray];
for(NSDictionary*dicinary) {
[ary1addObject:[AppappWithdic:dic]];
}
_apps= ary1;
}
return_apps;
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
returnself.apps.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return1;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
staticNSString* ID =@"app";
UITableViewCell*cell =[tableViewdequeueReusableCellWithIdentifier:ID];
App*app =_apps[indexPath.row];
cell.textLabel.text= app.name;
cell.detailTextLabel.text=app.download;
//下载图片
NSURL*url = [NSURLURLWithString:app.icon];
NSData*imageData = [NSDatadataWithContentsOfURL:url];
UIImage*image = [UIImageimageWithData:imageData];
cell.imageView.image=image;
returncell;
}
//UI很不流畅-》开子线程下载
//图片重复下载-》缓存
@end
#import
@interfaceApp :NSObject
@property(nonatomic,strong)NSString*name;
@property(nonatomic,strong)NSString*icon;
@property(nonatomic,strong)NSString*download;
+(instancetype)appWithdic:(NSDictionary*)dic;
@end
#import "App.h"
@implementation App
+(instancetype)appWithdic:(NSDictionary *)dic{
App *app = [[App alloc]init];
//KVC
[app setValuesForKeysWithDictionary:dic];
return app;
}
@end
网友评论