最近在做聊天页面,开始时用 _chatTableView.estimatedRowHeight = 25; _chatTableView.rowHeight = UITableViewAutomaticDimension;自动计算cell高度以省去计算的麻烦,但是我每次都要调用
NSIndexPath *index=[NSIndexPath indexPathForItem:_chatmessegeMutArray.count-1 inSection:0];
[_chatTableView scrollToRowAtIndexPath:index
atScrollPosition: UITableViewScrollPositionBottom
animated:NO];
来使tableview滚动到底部,由于cell的高度不固定使得tableview每次在调用scrollToRowAtIndexPath:atScrollPosition:方法时都要从新计算cell高度最后导致崩溃。
后来使用了UITableView+FDTemplateLayoutCell类来计算cell高度,因为它有一个缓存机制,可以记录第一次计算后cell的尺寸,而且用法较为简单
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [tableView fd_heightForCellWithIdentifier:@"LivingChatIDCell" configuration:^(id cell) {
//此处和(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath里面写相同的cell定制方法
}];
}
测试后发现好了很多,CPU还是有些高。然后从网上收了一些资料,将cell中的字视图直接添加到cell上面,放弃添加cell.contentView上面,然后内存占用率有原来一直在40%~80%降到10%~25%