在項目中 使用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;
}