利用RunLoop优化tableView
来源:互联网 发布:天数倒计时软件 编辑:程序博客网 时间:2024/06/07 07:14
代码示例
代码可以到这里下载,传送门demo
原文链接:http://www.qingpingshan.com/rjbc/ios/214222.html
iOS开发中,UITableView的优化一直是一个老生常谈的问题,除了常用的预加载,缓存等方法以外,其实利用RunLoop 的循环也可以实现超清大图的流畅加载,具体的使用方法我们利用一个demo来详细理解
首先,我们有一个需求,就是要从网络加载高清大图到UITableViewCell上,而且每个Cell上面加载多张图片,当cell数量过多的时候,我们需要保持流畅度和加载速度。
那么我们做一个简单的分析:
1,因为这里用到了Runloop循环,那么我们可以监听到runloop的每次循环,在每一次循环当中我们考虑去进行一次图片下载和布局。2,既然要在每次循环执行一次任务,我们可以先把所有图片加载的任务代码块添加到一个数组当中,每次循环取出第一个任务进行执行。*3,因为runloop在闲置的时候会自动休眠,所以我们要想办法让runloop始终处于循环中的状态。
好的,那么我们就可以开始考虑代码实现:
第一步,先把uitableview基本效果实现
@property (strong,nonatomic) UITableView* showImageTableView;//懒加载-(UITableView *)showImageTableView{ if (!_showImageTableView) { _showImageTableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain]; _showImageTableView.backgroundColor = [UIColor yellowColor]; _showImageTableView.delegate = self; _showImageTableView.dataSource = self; } return _showImageTableView;}//注册 [self.showImageTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ShowImageTableViewReusableIdentifier];//添加 [self.view addSubview:self.showImageTableView];//数据源代理#pragma mark- UITableViewDelegate-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ShowImageTableViewReusableIdentifier]; //每个cell中添加3张图片 for (int i = 0; i < 3; i++) { #这里是添加图片的方法 } return cell;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 399;}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 135;}
第二步,初始化可变数组用来存储任务(代码块)
typedef void(^SaveFuncBlock)();//存放任务的数组@property (nonatomic, strong) NSMutableArray *saveTaskMarr;//最大任务数(超过最大任务数的任务就停止执行)@property (nonatomic, assign) NSInteger maxTasksNumber;//任务执行的代码块@property (nonatomic, copy) SaveFuncBlock saveFuncBlock;-(NSMutableArray *)saveTaskMarr{ if (!_saveTaskMarr) { _saveTaskMarr = [NSMutableArray array]; } return _saveTaskMarr;} self.maxTasksNumber = 18;
第三步,新建cell添加图片的方法
-(void)addImageToCell:(UITableViewCell*)cell andTag:(NSInteger)tag{ UIImageView* cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(tag*(ImageWidth+5), 5, ImageWidth, ImageHeight)]; dispatch_async(dispatch_get_global_queue(0,0), ^{ NSData* imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img5.duitang.com/uploads/item/201312/14/20131214173346_iVKdT.jpeg"]]; dispatch_async(dispatch_get_main_queue(), ^{ cellImageView.image = [UIImage imageWithData:imageData]; [cell.contentView addSubview:cellImageView]; }); });}
第四步,将任务添加到数组保存
//添加任务进数组保存-(void)addTasks:(SaveFuncBlock)taskBlock{ [self.saveTaskMarr addObject:taskBlock]; //超过每次最多执行的任务数就移出当前数组 if (self.saveTaskMarr.count > self.maxTasksNumber) { [self.saveTaskMarr removeObjectAtIndex:0]; }}
第五步,在cellForRow方法当中,添加方法:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ShowImageTableViewReusableIdentifier]; for (int i = 0; i < 3; i++) { //添加任务到数组 __weak typeof(self) weakSelf = self; [self addTasks:^{ //下载图片的任务 [weakSelf addImageToCell:cell andTag:i]; }]; } return cell;}
第六步,监听RunLoop
第七步,也是最关键的步骤,使用定时器,保持RunLoop循环中。
//定时器,保证runloop一直处于循环中@property (nonatomic, weak) NSTimer *timer;self.timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(setRunLoop) userInfo:nil repeats:YES];//此方法主要是利用计时器事件保持runloop处于循环中,不用做任何处理-(void)setRunLoop{}
最后一步,在runLoop循环中去处理事件
//MARK: 回调函数//定义一个回调函数 一次RunLoop来一次static void Callback(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info){ ViewController * vcSelf = (__bridge ViewController *)(info); if (vcSelf.saveTaskMarr.count > 0) { //获取一次数组里面的任务并执行 SaveFuncBlock funcBlock = vcSelf.saveTaskMarr.firstObject; funcBlock(); [vcSelf.saveTaskMarr removeObjectAtIndex:0]; }}
好啦。到此为止,我们想要的效果就基本出来了,大家可以去试试:hushed:,同样的道理也可以应用于其他场景。
0 0
- 利用RunLoop优化tableView
- tableView- 性能优化,cell的循环利用三种方式
- tableView性能优化cell循环利用 && 自定义cell
- TableView优化
- tableview优化
- runloop优化主线程
- ios-RunLoop使tableView流畅化
- 利用runloop开启常驻线程
- TableView的优化
- TableView 性能优化
- TableView的优化
- 如何优化tableView
- iOS tableView滚动优化
- iOS之tableView优化
- TableView 性能优化
- TableView使用及优化
- tableView如何优化
- tableView cell性能优化
- SpringBoot入门系列:第一篇 Hello World
- 怎样健康喝水 这样喝水有助我们的健康
- gson日期转换bean Data
- 一个表中一次性分类返回满足不同条件的数据
- Codeforces484A (strings,bitmasks)
- 利用RunLoop优化tableView
- 最小生成树n*n模版
- TCP与UDP区别
- Ping命令的原理和使用
- JAVA读取等操作Excel文件
- URLConnection方法
- cuda性能调优
- base-64 字符数组的无效长度
- 蓝桥杯-黄金分割数