美文网首页
用户界面

用户界面

作者: 夜雨聲煩_ | 来源:发表于2018-04-16 00:33 被阅读0次

tableView优化

  1. 最基本,cell的重用。
  2. 固定行高的用self.tableView.rowHeight = 40;,而不用- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;。原因在于前者一次性固定所有 cell 高度,而后者需要每次调用绘制。
  3. 图片加载用异步,用 SDWebImage ,或者不在滑动过程中下载图片,在滑动过程中使用默认图片,滑动停止再下载。
    代码:
    // Only load cached images; defer new downloads until scrolling ends
    if (!appRecord.appIcon)
    {
        if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
        {
            [self startIconDownload:appRecord forIndexPath:indexPath];
        }
        // if a download is deferred or in progress, return a placeholder image
        cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];                
    }
    else
    {
       cell.imageView.image = appRecord.appIcon;
    }
    
  4. 尽量减少 cell 中控件,不要使用 addremove 添加和移除视图,而是初始化的时候添加,使用 hide 隐藏。
  5. cell 中控件过多时,在 drawRect 方法中异步绘制。
  6. 少用或者不用透明视图。
  7. 尽量不动态计算行高,如果行高不固定且复杂需要计算的时候,iOS8之前我们使用UITableView-FDTemplateLayoutCell这个第三方框架来帮我们计算。iOS8以后使用系统方法。如下:
    self.tableView.estimatedRowHeight  = 44; // 设置估算高度  
    self.tableView.rowHeight = UITableViewAutomaticDimension; // 告诉tableView我们cell的高度是自动的 
    
  8. 使用字典缓存动态行高。
    代码:
    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            NSNumber *height = [self.heightAtIndexPath objectForKey:indexPath];
            if(height)
            {
                return height.floatValue;
            }
            else
            {
                return 100;
            }
        }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return UITableViewAutomaticDimension;
    }
    
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSNumber *height = @(cell.frame.size.height);
        [self.heightAtIndexPath setObject:height forKey:indexPath];
    }
    
  9. 尽量只定义一种 cell。如果复用 n次,m个 cell 可能会复用 m*n 次。
  10. 不要把计算放在 cellForRow方法中。例如 cell 的高度,cell 所计划展示的文案,都放在 model 中。显得稍微有些不合理,如果是 MVVM 模式,放在 viewModel 中则十分合理。
    @interface DataEntity : NSObject
    //原始数据
    @property(copy, nonatomic) NSString *content;
    @property(copy, nonatomic) NSString *title;
    //Cell 高度
    @property(assign, nonatomic) CGFloat cellHeight;
    //真正显示的内容
    @property(strong, nonatomic) NSAttributedString       *showTitle;
    @property(strong, nonatomic) NSAttributedString *showContent;
    //计算高度
    - (void)calculateCellHeight;
    //创建、加工真正显示的内容
    - (void)setupShowTitileAndContent;
    @end
    
    在 viewModel 创建好时就计算好相关需要展示的属性,高度等,而不是在加载 tableview 时,可以以此优化。
  11. 滑动 UITableView 时,按需加载对应的内容
    代码:
    //按需加载 - 如果目标行与当前行相差超过指定行数,只在目标滚动范围的前后指定3行加载。
    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
        NSIndexPath *ip = [self indexPathForRowAtPoint:CGPointMake(0, targetContentOffset->y)];
        NSIndexPath *cip = [[self indexPathsForVisibleRows] firstObject];
        NSInteger skipCount = 8;
        if (labs(cip.row-ip.row)>skipCount) {
            NSArray *temp = [self indexPathsForRowsInRect:CGRectMake(0, targetContentOffset->y, self.width, self.height)];
            NSMutableArray *arr = [NSMutableArray arrayWithArray:temp];
            if (velocity.y<0) {
                NSIndexPath *indexPath = [temp lastObject];
                if (indexPath.row+3<datas.count) {
                    [arr addObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]];
                    [arr addObject:[NSIndexPath indexPathForRow:indexPath.row+2 inSection:0]];
                    [arr addObject:[NSIndexPath indexPathForRow:indexPath.row+3 inSection:0]];
                }
            } else {
                NSIndexPath *indexPath = [temp firstObject];
                if (indexPath.row>3) {
                    [arr addObject:[NSIndexPath indexPathForRow:indexPath.row-3 inSection:0]];
                    [arr addObject:[NSIndexPath indexPathForRow:indexPath.row-2 inSection:0]];
                    [arr addObject:[NSIndexPath indexPathForRow:indexPath.row-1 inSection:0]];
                }
            }
            [needLoadArr addObjectsFromArray:arr];
        }
    }
    
    记得在tableView:cellForRowAtIndexPath:方法中加入判断:
    //如果当前cell不在目标前后三行内,不必须加载,调用[cell clear] 清空
    if (needLoadArr.count>0&&[needLoadArr indexOfObject:indexPath]==NSNotFound) {
        [cell clear];
        return;
    }
    
  12. 不要在滚动视图使用 cornerRadius
    当图片内容不变时,圆角阴影等可以用以下代码优化:
    //开启shouldRasterize后,CALayer会被光栅化为bitmap,layer的阴影等效果也会被保存到bitmap中复用。
    self.layer.shouldRasterize = YES;  
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
    
    其他方法:使用圆角图片。预处理圆角图片。单色背景下加入镂空圆形图片。

相关文章

  • 2018-10-19

    主页面 注册界面 登入界面 管理员页面 普通用户页面 添加用户界面 查询用户界面 删除用户界面 修改用户界面 实体...

  • 操作系统学习笔记(八)

    操作系统用户界面(SHD) 用户界面的定义 用户界面的分类 操作界面典型的操作界面图形界面命令界面批处理 系统调用...

  • 人机界面设计

    人机界面风格: 1.语言界面:2.图形用户界面3.直接操纵用户界面4.多媒体用户界面5.多通道用户界面 设计问题:...

  • 用户界面

    tableView优化 最基本,cell的重用。 固定行高的用self.tableView.rowHeight =...

  • 用户界面

    概观 V-Ray是3ds Max的渲染插件。它包括两个渲染器: V-Ray,原始的全功能,强大的照片写实和特效渲染...

  • Python 常用库总结(3)

    图形用户界面 用来创建图形用户界面程序的库。 curses:内建的 ncurses 封装,用来创建终端图形用户界面...

  • Ubuntu18.04 关闭和开启图形用户界面

    关闭用户图形界面 开启用户图形界面

  • 交互设计常用英语缩写

    UI:user interface 用户界面 GUI:Graphics user interface 图形用户界面...

  • (8 Android) 用户界面UI设计

    (8 Android) 用户界面UI设计 3.1.1 Android界面视图类 Android 图形化的用户界面(...

  • app视觉设计艺术

    一、移动互联网产品在变化 UI=用户界面 用户的界面-从属关系-视觉传达与美化界面设计 用户与界面-交互关系-从视...

网友评论

      本文标题:用户界面

      本文链接:https://www.haomeiwen.com/subject/lrzjkftx.html