UITableView的执行顺序
1.它会调用代理方法确定有几行
[TableViewController numberOfSectionsInTableView:]
2.确定每行的表头高和表尾高(如果设定了HeardView和FooterView)
[TableViewController tableView:heightForHeaderInSection:]
[TableViewController tableView:heightForFooterInSection:]
3.确定每行有多少的cell
[TableViewController tableView:numberOfRowsInSection:]
4.然后确定每行cell的高度
[TableViewController tableView:heightForRowAtIndexPath:]
//伪代码如下
- (void)_updateSectionsCache
{
// uses the dataSource to rebuild the cache.
// if there's no dataSource, this can't do anything else.
// note that I'm presently caching and hanging on to views and titles for section headers which is something
// the real UIKit appears to fetch more on-demand than this. so far this has not been a problem.
// remove all previous section header/footer views
for (UITableViewSection *previousSectionRecord in _sections) {
[previousSectionRecord.headerView removeFromSuperview];
[previousSectionRecord.footerView removeFromSuperview];
}
// clear the previous cache
[_sections removeAllObjects];
if (_dataSource) {
// compute the heights/offsets of everything
const CGFloat defaultRowHeight = _rowHeight ?: _UITableViewDefaultRowHeight;
//1.调用numberOfSection 获取section的数量
const NSInteger numberOfSections = [self numberOfSections];
for (NSInteger section=0; section<numberOfSections; section++) {
const NSInteger numberOfRowsInSection = [self numberOfRowsInSection:section];
///2.循环section 获取heartView 和 footView的高度
UITableViewSection *sectionRecord = [[UITableViewSection alloc] init];
sectionRecord.headerTitle = _dataSourceHas.titleForHeaderInSection? [self.dataSource tableView:self titleForHeaderInSection:section] : nil;
sectionRecord.footerTitle = _dataSourceHas.titleForFooterInSection? [self.dataSource tableView:self titleForFooterInSection:section] : nil;
sectionRecord.headerHeight = _delegateHas.heightForHeaderInSection? [self.delegate tableView:self heightForHeaderInSection:section] : _sectionHeaderHeight;
sectionRecord.footerHeight = _delegateHas.heightForFooterInSection ? [self.delegate tableView:self heightForFooterInSection:section] : _sectionFooterHeight;
sectionRecord.headerView = (sectionRecord.headerHeight > 0 && _delegateHas.viewForHeaderInSection)? [self.delegate tableView:self viewForHeaderInSection:section] : nil;
sectionRecord.footerView = (sectionRecord.footerHeight > 0 && _delegateHas.viewForFooterInSection)? [self.delegate tableView:self viewForFooterInSection:section] : nil;
// make a default section header view if there's a title for it and no overriding view
if (!sectionRecord.headerView && sectionRecord.headerHeight > 0 && sectionRecord.headerTitle) {
sectionRecord.headerView = [UITableViewSectionLabel sectionLabelWithTitle:sectionRecord.headerTitle];
}
// make a default section footer view if there's a title for it and no overriding view
if (!sectionRecord.footerView && sectionRecord.footerHeight > 0 && sectionRecord.footerTitle) {
sectionRecord.footerView = [UITableViewSectionLabel sectionLabelWithTitle:sectionRecord.footerTitle];
}
if (sectionRecord.headerView) {
[self addSubview:sectionRecord.headerView];
} else {
sectionRecord.headerHeight = 0;
}
if (sectionRecord.footerView) {
[self addSubview:sectionRecord.footerView];
} else {
sectionRecord.footerHeight = 0;
}
CGFloat *rowHeights = malloc(numberOfRowsInSection * sizeof(CGFloat));
CGFloat totalRowsHeight = 0;
///3.获取numberOfRow的数量
for (NSInteger row=0; row<numberOfRowsInSection; row++) {
//4.循环rows 获取 每个row的高度
const CGFloat rowHeight = _delegateHas.heightForRowAtIndexPath? [self.delegate tableView:self heightForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]] : defaultRowHeight;
rowHeights[row] = rowHeight;
totalRowsHeight += rowHeight;
}
sectionRecord.rowsHeight = totalRowsHeight;
[sectionRecord setNumberOfRows:numberOfRowsInSection withHeights:rowHeights];
free(rowHeights);
[_sections addObject:sectionRecord];
}
}
}
-------------如果设置多行及重复执行以上方法-----------
5.以上信息确定完毕后及调用代理方法去获取cell
[TableViewController tableView:cellForRowAtIndexPath:]
6.返回cell的高度
[TableViewController tableView:heightForRowAtIndexPath:]
7.cell将要显示到屏幕上
[TableViewController tableView:willDisplayCell:forRowAtIndexPath:]
8.cell超出屏幕进行服用时及会调用两次
[TableViewController tableView:heightForRowAtIndexPath:]
然后在进行调用 5 . 6. 7 方法
如有错误请指出,谢谢