UITableView的默认标题视图

rei*_*ein 4 iphone objective-c uitableview

我有一个UITableView,它被设计为一个分组的tableView.在某些情况下,我需要为标题创建自定义视图,在其他情况下我需要默认视图.

创建自定义标头很容易 - 只需使用委托:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

但是,如果我想要默认的标题视图,我该从该委托返回什么?

我已经尝试返回nil,这只有在表视图被设置为Plain时才能正常工作.如果表格视图被设置为Grouped,那么当我返回nil时,默认标题会消失.

考虑到我有时需要自定义标题视图,如何获取默认标题?

编辑:

我想我已经找到了问题.在tableView:heightForHeaderInSection中:我执行以下操作:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if ([myArray count] == 0) return 80;
    else return [tableView sectionHeaderHeight];
}
Run Code Online (Sandbox Code Playgroud)

似乎[tableView sectionHeaderHeight]为我的标题返回了不正确的高度.它设置为10但应该是30左右.

小智 10

上述问题的正确答案是:

要返回默认视图,请返回nil

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   return nil;
}
Run Code Online (Sandbox Code Playgroud)

要返回默认标题高度,请返回-1

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   return -1;
}
Run Code Online (Sandbox Code Playgroud)

这对我有用!