UITableView的性能优化
来源:互联网 发布:阿里云ddos防御多少 编辑:程序博客网 时间:2024/04/27 09:39
UITableView的性能优化? 滑动的时候有种卡的感觉是为什么?怎么解决?
实际上只要针对性地优化一下:
UITableView是UIScrollView的子类,因此它可以自动响应滚动事件(一般为上下滚动)。
介绍完原理,接下来就开始优化吧。
static NSString *CellIdentifier = @"xxx"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; }
值得一提的是,cell被重用时,它内部绘制的内容并不会被自动清除,因此你可能需要调用setNeedsDisplayInRect:或setNeedsDisplay方法。
- (void)drawRect:(CGRect)rect { if (image) { [image drawAtPoint:imagePoint]; self.image = nil; } else { [placeHolder drawAtPoint:imagePoint]; } [text drawInRect:textRect withFont:font lineBreakMode:UILineBreakModeTailTruncation]; }
不过这样一来,你会发现选中一行后,这个cell就变蓝了,其中的内容就被挡住了。最简单的方法就是将cell的selectionStyle属性设为UITableViewCellSelection
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { if (!decelerate) { queue.maxConcurrentOperationCount = 5; } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { queue.maxConcurrentOperationCount = 5; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { queue.maxConcurrentOperationCount = 2; }
此外,自动载入更新数据对用户来说也很友好,这减少了用户等待下载的时间。例如每次载入50条信息,那就可以在滚动到倒数第10条以内时,加载更多信息:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if (count - indexPath.row < 10 && !updating) { updating = YES; [self update]; } }
// update方法获取到结果后,设置updating为NO
还有一点要注意的就是当图片下载完成后,如果cell是可见的,还需要更新图像:
NSArray *indexPaths = [self.tableView indexPathsForVisibleRows];for (NSIndexPath *visibleIndexPath in indexPaths) { if (indexPath == visibleIndexPath) { MyTableViewCell *cell = (MyTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath]; cell.image = image; [cell setNeedsDisplayInRect:imageRect]; break; }
最后还是前面所说过的insertRowsAtIndexPaths:withRowAnimation:方法,插入新行需要在主线程执行,而一次插入很多行的话(例如50行),会长时间阻塞主线程。而换成reloadData方法的话,瞬间就处理完了
- 优化UITableView的性能
- UITableView的性能优化
- UITableView的性能优化
- UITableView的性能优化
- UITableView的性能优化
- UItableView的性能优化
- UITableView的性能优化
- UITableView的性能优化
- UITableView的性能优化
- UITableView的性能优化
- UITableView的性能优化
- UITableView的性能优化问题
- 关于UITableView的性能优化
- 如何优化UITableView的性能
- 如何优化UITableView的性能
- UITableView的性能优化整理
- iOS 优化UITableView的性能
- 关于UITableView的性能优化
- 傅利曼数
- 进程的虚拟地址空间
- 1061. Dating (20)
- jzoj 1391. 【2012.02.25普及组】危险系数 解题报告
- java并发包学习系列:读写锁
- UITableView的性能优化
- Untiy Native Render Plugin在VR中的绘制
- 谈谈Java中的继承与组合
- jzoj 1390. 【2012.02.25普及组】牛车
- 闭包的实际应用(更新中。。。)
- html from
- java并发包学习系列:几个多线程控制工具类
- 单例模式之懒汉模式
- NYOJ 一种排序问题