沒有優(yōu)化前
#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很不流暢-》開子線程下載
//圖片重復(fù)下載-》緩存
@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