iOS多图下载案例(一)

作者: BEYOND黄 | 来源:发表于2017-05-30 13:36 被阅读25次

    没有优化前

    #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

    相关文章

      网友评论

        本文标题:iOS多图下载案例(一)

        本文链接:https://www.haomeiwen.com/subject/xykrfxtx.html