#import "HomeTableViewController.h"
#import "CZApp.h"
@interface HomeTableViewController ()
//plist文件数据的容器
@property(nonatomic,strong)NSArray *listArray;
//管理全局下载操作的队列
@property(nonatomic,strong)NSOperationQueue *opQueue;
//所有下载操作的缓存池
@property(nonatomic,strong)NSMutableDictionary *operationCache;
//所有图像的缓存
@property(nonatomic,strong)NSMutableDictionary *imageCache;
@end
@implementation HomeTableViewController
//懒加载
-(NSArray *)listArray{
if (_listArray ==nil) {
NSString *path = [[NSBundlemainBundle]pathForResource:@"videos.plist"ofType:nil];
NSArray *dictArray = [NSArrayarrayWithContentsOfFile:path];
//字典转模型
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;
//判断图片缓存池中是否有图片
if ([self.imageCacheobjectForKey:app.video_img]) {
NSLog(@"不用上网下载图片,从图片缓存池中取");
cell.imageView.image =self.imageCache[app.video_img];
return cell;
}
//显示占位图片
cell.dimageView.image = [UIImageimageNamed:@"user_default"];
//下载图片(重构剪出去)
[selfdownloadimage:indexPath];
return cell;
}
-(void)downloadimage:(NSIndexPath *)indexPath{
CZApp *app =self.listArray[indexPath.row];
//判断操作缓存池中是否存在下载图片的操作
if (self.operationCache[app.video_img]) {
NSLog(@"从缓存池中玩命下载......");
return;
}
//多线程异步
NSBlockOperation *downloadop = [NSBlockOperationblockOperationWithBlock:^{
NSLog(@"图片下载中......");
//1.下载图片
NSData *data = [NSDatadataWithContentsOfURL:[NSURLURLWithString:app.video_img]];
UIImage *image = [UIImageimageWithData:data];
//2.将下载的图片放在imageCache缓存中
if (image) {
[self.imageCachesetObject:image forKey:app.video_img];
//将操作从缓存池中移除
[self.operationCacheremoveObjectForKey:app.video_img];
}
//3.更新UI
[[NSOperationQueuemainQueue]addOperationWithBlock:^{
//刷新当前Cell
[self.tableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone];
}];
}];
//将操作放到队列里
[self.opQueueaddOperation:downloadop];
NSLog(@"操作的数量--------%lu",self.opQueue.operationCount);
//将操作添加到缓存池中(使用图片的URL作为Key)
[self.operationCachesetObject:downloadop forKey:app.video_img];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return110;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
//内存警告时,需要在这里进行一些内存清理工作,如果不处理,会被系统强制闪退
//清理图片的缓存
[self.imageCacheremoveAllObjects];
//清理操作的缓存
[self.operationCacheremoveAllObjects];
//取消下载队列里的任务
[self.opQueuecancelAllOperations];
}
@end
@interface HomeTableViewCell : UITableViewCell
@property (nonatomic,retain)UILabel *label;
@property (nonatomic,retain)UILabel *detailLabel;
@property (nonatomic,retain)UIImageView *dimageView;
@end
@implementation HomeTableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[supersetSelected:selected animated:animated];
// Configure the view for the selected state
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
[selfinitUI];
}
returnself;
}
//定制Cell内容
- (void)initUI{
_label = [[UILabelalloc]initWithFrame:CGRectZero];
_label.font = [UIFontsystemFontOfSize:24];
[self.contentViewaddSubview:_label];
_detailLabel = [[UILabelalloc]initWithFrame:CGRectZero];
_detailLabel.font = [UIFontsystemFontOfSize:16];
[self.contentViewaddSubview:_detailLabel];
_dimageView = [[UIImageViewalloc]init];
[self.contentViewaddSubview:_dimageView];
}
//改变系统样式位置
- (void)layoutSubviews{
[superlayoutSubviews]; //一定要调用父类,否则无法排版
//修改label位置
_label.frame =CGRectMake(200,20, 200,40);
_detailLabel.frame =CGRectMake(200,65, 200,20);
_dimageView.frame =CGRectMake(5,5, 100,90);
}
@end
@interface CZApp : NSObject
@property(nonatomic,copy)NSString *video_img;
@property(nonatomic,copy)NSString *video_title;
@property(nonatomic,copy)NSString *video_subTitle;
@property(nonatomic,copy)NSString *video_url;
+(instancetype)appWithDict:(NSDictionary *)dict;
@end
@implementation CZApp
+(instancetype)appWithDict:(NSDictionary *)dict{
CZApp *app =[[selfalloc]init];
[app setValuesForKeysWithDictionary:dict];
return app;
}
@end