UITableView中herderView的复用

来源:互联网 发布:linux安卓 编辑:程序博客网 时间:2024/05/29 12:44

在iOS开发中,很多同行似乎只使用了cell的复用,但对于headerView却极少使用复用。下面就给大家说下headerView的复用,直接上码看吧。


方法1

使用系统自带的headerView

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{    UITableViewHeaderFooterView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"section"];    if (!headView)    {        headView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"section"];    }        headView.textLabel.text = [NSString stringWithFormat:@"section %ld", section];        return headView;}


方法2

使用自定义的headerView(继承UITableViewHeaderFooterView

2-1:导入头文件

#import "HeaderView.h"

2-2:注册headerView

[self.mainTableView registerClass:[HeaderView class] forHeaderFooterViewReuseIdentifier:@"section"];

2-3:使用

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{    HeaderView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"section"];    headView.textLabel.text = [NSString stringWithFormat:@"section %ld", section];        return headView;}




0 0
原创粉丝点击