在自定义高度随着数据模型动态变化的tableViewCell的时候,你们一般是如何进行封装并且计算出cell高度的呢?
我的做法是:
一个控制器VC,一个cell,一个数据模型,一个装有cell所有子控件的frame的类。在VC获取到数据模型后,通过frame类计算好cell的子控件的frame和cell高度。然后在cell中根据数据模型和frame为子控件赋值和设置frame。
不知道各位大神是怎么做的?
还有,在自定义内容动态增减的cell,比如微博cell里面的图片,有时候没有图片,有时候有一张,有时候有多张。这种情况是怎么处理的?是先新建许多imageVeiw,然后根据数据动态隐藏显示,还是根据数据再来动态创建imageVeiw?
最好有个Demo,供小弟参考一下,感激不尽
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Message *msgObj = _items[(NSUInteger) indexPath.row];
NSString *msg = msgObj.content;
CGFloat maxWidth = CGRectGetWidth(tableView.bounds);
CGFloat msgCellWidth = maxWidth - MessageCellAvatarPadding * 2 - AVATAR_HEIGHT - MessageCellPadding-20;
CGFloat height = [MsgCell heightForMessage:msg constrainedToWidth:msgCellWidth];
return height+20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MsgCell *msgItemCell = [tableView dequeueReusableCellWithIdentifier:kMsgCellId forIndexPath:indexPath];
Message *msg = self.items[(NSUInteger) indexPath.row];
[msgItemCell setMessage:msg];
return msgItemCell;
}