很多人肯定都很困惑,UITableView如何算高度才最高效。
首先我们来理解一下UITableView的实现原理:
UITableView使用了重用机制,在其回调当中优先掉用
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 方法;然后再调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法;
这对于很多喜欢在cellForRowAtIndexPath:方法中自定义cell,再计算高度的开发者而言很蛋疼;因为他在没创建cell之前,根本就不知道高度。
按照一般的开发思路,应该是先创建自定义的cell,然后设置内容、布局;然后再返回高度。但apple的实现思路确是:先把位置定好,再在对应的位置放cell。
特别注意:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NS