Customise UITableViewHeaderFooterView through XIB


Create a UIView class through XIB, and change it as to be the subclass of UITableViewHeaderFooterView.


Remember: Change the background color of the UITableViewHeaderFooterView to be "Default" in the XIB.

ELSE, you will get this warning:

"Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead."


then, HOW to change the background color of UITableViewHeaderFooterView:

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.backgroundView = ({
        UIView * view = [[UIView alloc] initWithFrame:self.bounds];
        view.backgroundColor = [UIColor whiteColor];
        view;
    });
}

And, How to use this customised UITableViewHeaderFooterView:

Claim it first:

[self.tableView registerNib:[UINib nibWithNibName:@"HeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"HeaderViewIdentifier"];

reload the table view:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 80;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HeaderView *view = (HeaderView *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HeaderViewIdentifier"];
    
    return view;
}





你可能感兴趣的:(iOS,Dev)