在開發(fā)的過程中属愤,需要在viewForHeaderInSection的方法中創(chuàng)建視圖女器,并且賦值,動態(tài)獲取高度住诸,但是在heightForHeaderInSection賦值的時候發(fā)現(xiàn)高度還是之前的驾胆。
原因是:視圖執(zhí)行的順序是先
從上圖可以看出涣澡,是先執(zhí)行heightForHeaderInSection,再執(zhí)行viewForHeaderInSection丧诺。也就是入桂,先執(zhí)行高度,再執(zhí)行視圖驳阎。
但是抗愁,為了高度能自適應(yīng)視圖內(nèi)容的多少,需要在視圖加載完成后呵晚,重新執(zhí)行高度賦值的方法:
先執(zhí)行heightForHeaderInSection
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CGFloat hei = _headerHei > 0 ? _headerHei : 75;
NSLog(@"當(dāng)前方法名: %@蜘腌,當(dāng)前分區(qū):%ld",NSStringFromSelector(_cmd), (long)section);
return hei;
}
再執(zhí)行viewForHeaderInSection
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
self.HeaderView = [HuScoreRigHeaderV new];
ContentListModel * listModel = self.rightArray[section];
self.HeaderView.contentListM = listModel;
//自適應(yīng)高度:
_headerHei = [self.HeaderView scoreRightCellHeight];
self.HeaderView.frame = CGRectMake(0, 0, HHBWIDTH / 3 * 2 - 10, _headerHei);
NSLog(@"當(dāng)前方法名: %@,當(dāng)前分區(qū) %ld",NSStringFromSelector(_cmd), (long)section);
return self.HeaderView ;
}
解決方案:
在viewForHeaderInSection被調(diào)用后饵隙,讓系統(tǒng)重新調(diào)用一次heightForHeaderInSection撮珠。因此,在viewDidAppear中讓它重新調(diào)用一次金矛。
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.rigTableView reloadData];
}
最后的執(zhí)行順序為:
???????????????????????分割線???????????????????????
按照上面的寫法芯急,遇到了以下問題:
(1)通過cell所在的section獲取sectionHeader的時候,視圖為nil驶俊。之前sectionView繼承UIView娶耍,在后面需要通過cell所在的section獲取sectionHeader的時候,發(fā)現(xiàn)headerViewForSection:section方法獲取的為nil饼酿。
點進去發(fā)現(xiàn)這個方法:
- (nullable UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (nullable UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
所以榕酒,在自定義sectionHeaderView的時候繼承自UITableViewHeaderFooterView。
(2)在viewForHeader初始化方法錯誤:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
ContentListModel *listModel = self.rightArray[section];
HuScoreRigHeaderV *headV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headv"];
if (!headV) {
headV = [[HuScoreRigHeaderV alloc] initWithReuseIdentifier:@"headv"];
}
headV.delegate = self;
headV.contentListModel = listModel;
return headV;
}
(3)注意plain和group對viewForHeader的影響嗜湃。如果使用的是plain的話奈应,會遇到當(dāng)sectionHeader上的內(nèi)容過多的時候,會發(fā)現(xiàn)sectionHeader遮擋住cell购披。主要是plain默認不是懸浮滑動的狀態(tài)杖挣,它是相對gruop的sectionHeader來說是固定的,初始化的時候刚陡,樣式改為group即可解決問題惩妇。
改進方法獲取高度:
在開發(fā)的過程中,需要在 viewForHeaderInSection的方法中創(chuàng)建視圖筐乳,并且賦值歌殃,動態(tài)獲取高度,但是在heightForHeaderInSection賦值的時候發(fā)現(xiàn)高度還是之前的蝙云。
解決方案:
在數(shù)據(jù)請求回來的時候氓皱,計算自適應(yīng)的高度,在model中新增“高度”的屬性,將計算的高度直接賦值給model的這個屬性波材。
數(shù)據(jù)解析的時候股淡,直接在操作數(shù)據(jù)源獲取高度:
for (ItemListModel *itemListModel in leftArr) {
for (int i = 0; i < itemListModel.itemContentList.count; i++) {
ContentListModel *model = itemListModel.itemContentList[i];
//自適應(yīng)高度:
CGFloat contentHeight = [UIKitTool caculateHeight:model.content sizeOfWidth:kLableWidth - 30 withFont:[UIFont customFontWithName:@"PingFangSC-Regular" size:14.0]];
model.height = 8 + contentHeight + 5
}
在heightForHeaderInSection的方法中直接區(qū)model中的屬性即可。
完整代碼:
//先執(zhí)行高度:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
ContentListModel *listModel = self.rightArray[section];
CGFloat hei = listModel.height > 0 ? listModel.height : 75;
return hei;
}
//再執(zhí)行視圖
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
ContentListModel *listModel = self.rightArray[section];
HuScoreRigHeaderV *headV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headv"];
if (!headV) {
headV = [[HuScoreRigHeaderV alloc] initWithReuseIdentifier:@"headv"];
}
headV.delegate = self;
headV.contentListModel = listModel;
return headV;
}