码迷,mamicode.com
首页 > 移动开发 > 详细

【IOS开发】UITableView的性能优化

时间:2015-09-18 01:57:35      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

一、.重用cell

  在数据源方法中,在可见的页面重复绘制

OC方法中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

SWIFT方法
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

 因此 ,可以在该方法中做一些处理操作 

OC处理操作
 static NSString *  ReusedCellID = "ReusedCellID";
  UITableViewCell * cell = tableView.dequeueReusableCellWithIdentifier( ReusedCellID, forIndexPath: indexPath)

swift的处理操作
 private let ReusedCellID = "ReusedCellID"
 let cell = tableView.dequeueReusableCellWithIdentifier( ReusedCellID, forIndexPath: indexPath)

   这样,就可以防止cell无限的被创建,重用cell

  

二、cell的图片异步加载

当设置图片异步加载之后,cell 的图片就放在了子线程中执行,但是,如果此时下滑,也会出现卡顿的现象

解决办法:

  异步下载,在以下两个方法中下载图片、  

// 当结束拖拽的时候
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

// 减速的时候
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

 

具体操作:  

//获取可见部分的对象
NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
for (NSIndexPath *indexPath in visiblePaths)
{
  //获取的dataSource里面的对象,并且判断加载完成的不需要再次异步加载
  <code>
}


同时在cell绘制中也做限制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
if (self.tableView.dragging == NO && self.tableView.decelerating == NO) { //开始异步加载图片 <code> }
}

     
三、尽量少用透明的图层

  内部的渲染机制

 

【IOS开发】UITableView的性能优化

标签:

原文地址:http://www.cnblogs.com/gaox97329498/p/4818096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!