- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
view.backgroundColor = [UIColor redColor];
}
想改變UITableView的footerView的背景色,上面是最容易的方法,但并沒(méi)有用吞瞪。
于是用了這個(gè)方法
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *headerView = [UIView new];
headerView.backgroundColor = [UIColor redColor];
return headerView;
}
能有效箩朴。
因?yàn)槲翼?xiàng)目中
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 100;
}
有很多個(gè)section,就在以上方法中做了優(yōu)化岗喉,用了重用機(jī)制
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
static NSString *ID = @"headerViewIdentifier";
UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
if (footerView == nil) {
footerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:ID];
}
[footerView.contentView setBackgroundColor:[UIColor blueColor]];
return footerView;
}
還有一點(diǎn)有必要說(shuō)明,如果要要控制某個(gè)section不展示headerView,下面方法是沒(méi)有用的
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
static NSString *ID = @"headerViewIdentifier";
UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
if (footerView == nil) {
footerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:ID];
}
[footerView.contentView setBackgroundColor:[UIColor blueColor]];
return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
if (section == 0) {
return 0;
}
return 10;
}
正確做法:
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
if (section == 0) {
return nil;
}
static NSString *ID = @"headerViewIdentifier";
UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
if (footerView == nil) {
footerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:ID];
}
[footerView.contentView setBackgroundColor:[UIColor blueColor]];
return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 10;
}
說(shuō)明- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section方法只能影響footerView的高度,不能控制foooterView是否顯示(就是return 0 footerView會(huì)顯示默認(rèn)高度18)
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
if (self.type == FDOrderSearchViewControllerTypeSearchResult) {
static NSString *ID = @"headerViewIdentifier";
UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
if (footerView == nil) {
footerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:ID];
}else{
NSLog(@"===%f",footerView.frame.size.height);
}
[footerView.contentView setBackgroundColor:[UIColor blueColor]];
return footerView;
}
return nil;
}
打印結(jié)果.png