nsoperation封装_封装思想之:NSoperation的自定义实现

1 #import "HMApp.h"

2 #import "HMViewController.h"

3 #import "HMDownloadOperation.h"

4

5 @interface HMViewController ()

6 @property (nonatomic, strong) NSArray *apps; //用来存储数据结构Model

7 @property (nonatomic, strong) NSOperationQueue *queue;//用来全局控制自定义NSOperasion的队列

8 /** key:url value:operation对象*/

9 @property (nonatomic, strong) NSMutableDictionary *operations;10 //这里定义一个字典类的操作,是为了实现每一个Cell的Operation与网页中的url地址进行绑定,定义她的字典主要是为了考虑图片正在下载而没有加载到images的情况

11 /** key:url value:image对象*/

12 @property (nonatomic, strong) NSMutableDictionary *images;13 //cell中操作的赋值实现,如果对cell进行封装应该都也cell封装类里面

14 @end

15

16 @implementationHMViewController17

18 - (NSArray *)apps19 {20 if (!_apps) {21 NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist"ofType:nil]];22

23 NSMutableArray *appArray =[NSMutableArray array];24 for (NSDictionary *dict indictArray) {25 HMApp *app =[HMApp appWithDict:dict];26 [appArray addObject:app];27 }28 _apps =appArray;29 }30 return_apps;31 }32

33 - (NSOperationQueue *)queue34 {35 if (!_queue) {36 _queue =[[NSOperationQueue alloc] init];37 _queue.maxConcurrentOperationCount = 3; //最大并发数 == 3

38 }39 return_queue;40 }41

42 - (NSMutableDictionary *)operations43 {44 if (!_operations) {45 _operations =[NSMutableDictionary dictionary];46 }47 return_operations;48 }49

50 - (NSMutableDictionary *)images51 {52 if (!_images) {53 _images =[NSMutableDictionary dictionary];54 }55 return_images;56 }57

58 - (void)viewDidLoad59 {60 [super viewDidLoad];61

62 }63

64 #pragma mark - 数据源方法

65 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section66 {67 returnself.apps.count;68 }69

70 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath71 {72 static NSString *ID = @"app";73 UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:ID];74 if (!cell) {75 cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];76 }77

78 HMApp *app =self.apps[indexPath.row];79 cell.textLabel.text =app.name;80 cell.detailTextLabel.text =app.download;81

82 //显示图片83 //保证一个url对应一个HMDownloadOperation84 //保证一个url对应UIImage对象

85

86 UIImage *image =self.images[app.icon];87 if (image) { //缓存中有图片

88 cell.imageView.image =image;89 } else { //缓存中没有图片, 得下载

90 cell.imageView.image = [UIImage imageNamed:@"57437179_42489b0"];91

92 HMDownloadOperation *operation =self.operations[app.icon];93 if (operation) { //正在下载94 //... 暂时不需要做其他事

95

96 } else { //没有正在下载97 //创建操作

98 operation =[[HMDownloadOperation alloc] init];99 operation.url =app.icon;100 operation.delegate =self;101 operation.indexPath =indexPath;102 [self.queue addOperation:operation]; //异步下载

103

104 self.operations[app.icon] =operation;105 }106 }107

108 //SDWebImage : 专门用来下载图片

109 returncell;110 }111

112 #pragma mark - HMDownloadOperationDelegate

113 - (void)downloadOperation:(HMDownloadOperation *)operation didFinishDownload:(UIImage *)image114 {115 //1.移除执行完毕的操作

116 [self.operations removeObjectForKey:operation.url];117

118 if(image) {119 //2.将图片放到缓存中(images)

120 self.images[operation.url] =image;121

122 //3.刷新表格

123 [self.tableView reloadRowsAtIndexPaths:@[operation.indexPath] withRowAnimation:UITableViewRowAnimationNone];124

125 //3.将图片写入沙盒126 //NSData *data = UIImagePNGRepresentation(image);127 //[data writeToFile:@"" atomically:];

128 }129

130 }131

132 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView133 {134 //开始拖拽135 //暂停队列

136 [self.queue setSuspended:YES];137 }138

139 - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset140 {141 [self.queue setSuspended:NO];142 }143

144 @end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值