How to change height of grouped UITableView header?

前端 未结 10 644
野性不改
野性不改 2020-12-07 09:22

I know how to change the height of the section headers in the table view. But I am unable to find any solution to change the default spacing before the first section.

相关标签:
10条回答
  • 2020-12-07 10:23

    you can use viewForHeaderInSection and return a view with any height.

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
    
        int height = 30 //you can change the height 
        if(section==0)
        {
           UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, height)];
    
           return view;
        }
    }
    
    0 讨论(0)
  • 2020-12-07 10:24

    Example of viewForHeaderInSection:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 118)];
    view.backgroundColor = COLOR_DEFAULT;
    
    NSString* key = [self.tableKeys objectAtIndex:section];
    NSArray *result = (NSArray*)[self.filteredTableData objectForKey:key];
    SZTicketsResult *ticketResult = [result objectAtIndex:0];
    
    UIView *smallColoredView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 3)];
    smallColoredView.backgroundColor = COLOR_DEFAULT_KOSTKY;
    [view addSubview:smallColoredView];
    
    UIView *topBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 8, 320, 40)];
    topBackgroundView.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:248.0/255.0 blue:174.0/255.0 alpha:1];
    [view addSubview:topBackgroundView];
    
    UILabel *totalWinnings = [[UILabel alloc] initWithFrame:CGRectMake(10, 8, 300, 40)];
    totalWinnings.text = ticketResult.message;
    totalWinnings.minimumFontSize = 10.0f;
    totalWinnings.numberOfLines = 0;
    totalWinnings.backgroundColor = [UIColor clearColor];
    totalWinnings.font = [UIFont boldSystemFontOfSize:15.0f];
    [view addSubview:totalWinnings];
    
    UIView *bottomBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 55, 320, 58)];
    bottomBackgroundView.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:248.0/255.0 blue:174.0/255.0 alpha:1];
    [view addSubview:bottomBackgroundView];
    
    UILabel *numberOfDraw = [[UILabel alloc] initWithFrame:CGRectMake(10, 55, 290, 58)];
    numberOfDraw.text = [NSString stringWithFormat:@"sometext %@",[ticketResult.title lowercaseString]];;
    numberOfDraw.minimumFontSize = 10.0f;
    numberOfDraw.numberOfLines = 0;
    numberOfDraw.backgroundColor = [UIColor clearColor];
    numberOfDraw.font = [UIFont boldSystemFontOfSize:15.0f];
    [view addSubview:numberOfDraw];
    
    return view;
    
    0 讨论(0)
  • 2020-12-07 10:25

    It appears that I can't set a table header view with height of 0. I ended up doing the following:

    - (void)viewWillAppear:(BOOL)animated{
        CGRect frame = self.tableView.tableHeaderView.frame;
        frame.size.height = 1;
        UIView *headerView = [[UIView alloc] initWithFrame:frame];
        [self.tableView setTableHeaderView:headerView];
    }
    
    0 讨论(0)
  • 2020-12-07 10:25

    In Swift 4

    Remove extra top padding in grouped tableview.

    Here height is given 1 as minimal height for the section header because you cannot give 0 as tableview will take it default top margin if assigned zero height.

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 1
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    
    0 讨论(0)
提交回复
热议问题