#import "HomeTableViewController.h"
#import "CZApp.h"
@interface Hometableviewcontroller ()
Container for plist file data
@property (Nonatomic,strong) Nsarray *listarray;
Manage queues for global download operations
@property (Nonatomic,strong) Nsoperationqueue *opqueue;
Cache pool for all download operations
@property (Nonatomic,strong) nsmutabledictionary *operationcache;
Caching of all images
@property (Nonatomic,strong) nsmutabledictionary *imagecache;
@end
@implementation Hometableviewcontroller
Lazy load
-(Nsarray *) listarray{
if (_listarray ==nil) {
NSString *path = [[nsbundlemainbundle]pathforresource:@ ' videos.plist ' oftype:nil];
Nsarray *dictarray = [Nsarrayarraywithcontentsoffile:path];
Dictionary Turn model
Nsmutablearray *muarray = [[Nsmutablearrayalloc]init];
For (nsdictionary *dictin Dictarray) {
Czapp *app = [Czappappwithdict:dict];
[Muarray Addobject:app];
}
_listarray = Muarray;
}
Return_listarray;
}
-(Nsmutabledictionary *) operationcache{
if (_operationcache ==nil) {
_operationcache = [Nsmutabledictionarydictionary];
}
Return_operationcache;
}
-(Nsmutabledictionary *) imagecache{
if (_imagecache ==nil) {
_imagecache = [[Nsmutabledictionaryalloc]init];
}
Return_imagecache;
}
-(Nsoperationqueue *) opqueue{
if (_opqueue ==nil) {
_opqueue = [[Nsoperationqueuealloc]init];
}
Return_opqueue;
}
-(void) Viewdidload {
[Superviewdidload];
}
#pragma mark-table View data source
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {
RETURN1;
}
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {
Returnself.listArray.count;
}
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {
Hometableviewcell *cell=[tableviewdequeuereusablecellwithidentifier:@ "Cell"];
if (Cell==nil) {
cell=[[hometableviewcellalloc]initwithstyle:uitableviewcellstyledefaultreuseidentifier:@ "Cell"];
Cell.selectionstyle=uitableviewcellselectionstylenone;
}
Czapp *app =self.listarray[indexpath.row];
Cell.label.text = App.video_title;
Cell.detailLabel.text = App.video_subtitle;
Determine if there are pictures in the picture cache pool
if ([self.imageCacheobjectForKey:app.video_img]) {
NSLog (@ "Do not download pictures on the Internet, from the image buffer pool);"
Cell.imageView.image =self.imagecache[app.video_img];
return cell;
}
Show placeholder Pictures
Cell.dimageView.image = [uiimageimagenamed:@ "User_default"];
Download picture (refactoring cut out)
[Selfdownloadimage:indexpath];
return cell;
}
-(void) Downloadimage: (Nsindexpath *) indexpath{
Czapp *app =self.listarray[indexpath.row];
To determine if there is an action in the Operation cache pool for downloading pictures
if (Self.operationcache[app.video_img]) {
NSLog (@ "Desperate to download from the cache pool ...");
Return
}
Multithreading asynchronous
Nsblockoperation *DOWNLOADOP = [nsblockoperationblockoperationwithblock:^{
NSLog (@ "Picture download ...");
1. Download pictures
NSData *data = [nsdatadatawithcontentsofurl:[nsurlurlwithstring:app.video_img]];
UIImage *image = [Uiimageimagewithdata:data];
2. Put the downloaded picture in the Imagecache cache
if (image) {
[Self.imageCachesetObject:image forKey:app.video_img];
To remove an action from the cache pool
[Self.operationCacheremoveObjectForKey:app.video_img];
}