一、cell自适应高度

 self.tableView.estimatedRowHeight = 60;//预设高度
//或者:estimatedHeightForRowAtIndexPath代理方法中设置
    self.tableView.rowHeight = UITableViewAutomaticDimension;

设置的AutoLayout 约束必须让 cell 的 contentView知道如何自动伸展。关键点是 contentView 的 4 个边都要设置连接到内容的约束,并且内容是会动态改变尺寸的。其实只要记住,我们在设置约束时只要能让contentView能被内容撑起来就可以了。
参考http://www.jianshu.com/p/bbe8094e147d

缓存cell的高度,代码如下:

@property (nonatomic, strong) NSMutableDictionary *heightAtIndexPath;//缓存高度所用字典
#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
      NSNumber *height = [self.heightAtIndexPath objectForKey:indexPath];
      if(height)
        {
            return height.floatValue; 
         }else 
         {
             return 100;
        }

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
   {
         NSNumber *height = @(cell.frame.size.height); 
         [self.heightAtIndexPath setObject:height forKey:indexPath];
  }

就是用一个字典做容器,在cell将要显示的时候在字典中保存这行cell的高度。然后在调用estimatedHeightForRowAtIndexPath方法时,先去字典查看有没有缓存高度,有就返回,没有就返回一个大概高度。这段代码其实可以写在viewController的基类里面,这样写一遍就可以每个地方都能缓存cell的高度了
(选自http://www.jianshu.com/p/64f0e1557562)
这篇文章的demo的Github地址:UITableViewCellHeightDemo

二、优化--------(待增加、修改)

//下面的2段代码可以加上 可以有效提高流畅度
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

1使用不透明视图。
极大地提高渲染的速度,如非必要,可以将table cell及其子视图的opaque属性设为YES(默认值)。其中的特例包括背景色,它的alpha值应该为1(例如不要使用clearColor);图像的alpha值也应该为1,或者在画图时设为不透明。

2、复用cell

3、不要阻塞主线程。最常见的就是网络请求、更新数据时,整个界面卡住不动,这种现象的原因就是主线程执行了耗时很长的函数或方法,在其执行完毕前,无法绘制屏幕和响应用户请求,解决办法就是使用多线程,让子线程去执行这些函数或方法。(待了解、、、)

4、自动载入更新数据对用户来说也很友好,这减少了用户等待下载的时间,例如每次载入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]

你可能感兴趣的:(表)