Obtaining the UITableViewHeaderFooterView of a static UITableView

和自甴很熟 提交于 2019-12-24 02:09:29

问题


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


来源:https://stackoverflow.com/questions/13556086/obtaining-the-uitableviewheaderfooterview-of-a-static-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!