自定义UITableView的Header和Footer

来源:互联网 发布:java互联网金融 编辑:程序博客网 时间:2024/05/04 14:05

Apple为UITaleView的Section提供了带标题的Header和Footer,只需使用UITableViewDataSource的

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

方法就可以为为每个section的Header或者Footer添加标题,不再需要其他的操作。

如果你不满意仅有标题的Header和Footer,可以为Header和Footer自定义View。

自定义Header的View,实现UITableViewDelegate的:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {    UIView *headerView ...    return headerView;} 

自定义Footer的View,实现UITableViewDelegate的:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {    UIView *footerView ...    return footerView;} 

除了实现UITableViewDelegate的方法返回自定义View之外,必须实现UITableViewDelegate的

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

方法,明确返回Header和Footer的高,才能使自定义的View显示出来。



作者:r_lin
链接:http://www.jianshu.com/p/02f89a5700c7
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
原创粉丝点击