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;
}