How to recreate the default UIView the same as the default tableView:viewForHeaderInSection:?

后端 未结 4 636
面向向阳花
面向向阳花 2021-02-04 16:58

I tried to implement the

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

to get the text label of the

4条回答
  •  青春惊慌失措
    2021-02-04 17:45

    You can get the default UITableView section headers by implementing the following methods in your tableview class:

    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {   
        return nil;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 22; //returning 0 hides section headers
    }
    

    This might be particularly useful if you use a UISearchDisplayController in your app, where you want the search results to be displayed in a simple UITableViewStylePlain fashion instead of, let's say, the UITableViewStyleGrouped of your customized main tableview. You could then implement something like this:

    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {   
        if ([self.searchDisplayController.searchBar isFirstResponder]) return nil;
        //create and return your custom UIView here
    }
    

提交回复
热议问题