美文网首页iOS那些事
自定义Cell高度 封装在模型中

自定义Cell高度 封装在模型中

作者: HOULI | 来源:发表于2016-08-13 22:43 被阅读15次

    在项目中 使用tableView比较多,cell的高度有的时候不是固定的,所以需要根据模型计算高度,为了考虑效率和代码易读,我们把cell上控件的frame 封装到模型中,
    1、给所有控件的frame
    2、cell的高度;

      @interface HLCellModel : NSObject
      //****** frame *******
       /**
       *  文字 图片数据
       */
      @property(nonatomic,assign)CGRect iconFrame;
      /**
       *  cell高度
       */
      @property(nonatomic,assign) float cellHeight;
      @end
    
      @implementation HLCellModel
      //重写模型cellHeight属性的get方法
      -(float)cellHeight
     {
        if (_cellHeight == 0) {
        // ...计算所有子控件的frame cell的高度
         }
        return _cellHeight;
     }
     @end
    
     //在tableview上使用
     -(CGFloat)tableView:(UITableView *)tableView                                        heightForRowAtIndexPath:(NSIndexPath *)indexPath
     {
       HLCellModel * cellModel = [self.dataSourceArray objectAtIndex:indexPath.row];
        return cellModel.cellHeight;
     }

    相关文章

      网友评论

        本文标题:自定义Cell高度 封装在模型中

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