被忽略的UITableViewHeaderFooterView

本文介绍了UITableView中如何实现HeaderView的复用,通过设置Identifier来节省内存占用,避免每次加载都重新初始化HeaderView,提供了具体的实现代码。

UITableView 我们知道cell 可以根据标记Identifier 可以进行重用,节省内存。

但是我们很多时候 我们在设置 headerView  FootView 的时候 就是没有用到重用了!每次都是初始化了。

也不说这样写不对!得看实际情况。

 

未使用到headerView复用

 

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
{  
    UILabel *titleLabel = [[UILabel alloc] init];  
    titleLabel.frame = CGRectMake(5, 0, 200, 30);  
    titleLabel.textColor = [UIColor purpleColor];  
    titleLabel.text = @"xixi";  
    return titleLabel;  
}  


想要做到重用,iOS也提供一个类似的cell 也有提供标记Identifier

 

 

 

 

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
{  
  
    static NSString *headerSectionID = @"cityHeaderSectionID";  
    UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerSectionID];  
    if (headerView == nil)  
    {  
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:headerSectionID];  
        UILabel *titleLabel = [[UILabel alloc] init];  
        titleLabel.frame = CGRectMake(5, 0, 200, 30);  
        titleLabel.textColor = [UIColor purpleColor];  
        titleLabel.tag = 100;  
        [headerView addSubview:titleLabel];  
    }  
   
    return headerView;  
}   

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值