Obtaining the UITableViewHeaderFooterView of a sta

2019-07-15 06:06发布

问题:

I have a Storyboard which contains a UITableViewController. The table view is static and contains two sections. The table view was setup entirely in IB (including the header and footer text of the table view sections) - I have not implemented any table view delegate methods in my view controller.

If I attempt to get a reference to the UITableViewHeaderFooterView for a given section it always returns nil.

UITableViewHeaderFooterView* header =[self.tableView headerViewForSection:0];

When I run the app I can see the header and footer text that I set in IB so I know those views are there. I just can't figure out how to access them programmatically.

Any help would be much appreciated, CS

回答1:

Actually you'll need to specify the table footer view, and pass it to the table view using its delegate.

For example, suppose you have defined a IBOutlet UIView named tableFooterView. Then in the following delegate method, you can pass your tableFooterView to your table view:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   // custom view for footer. will be adjusted to default or specified footer height
{
    return self.tableFooterView;
}

Also you'll need to implement the following method to set the height of the table footer view:

 -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
 {
    return self.tableFooterView.frame.size.height;
 }