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

后端 未结 4 589
面向向阳花
面向向阳花 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:36

    Although this is not exactly right, I'm posting this in hopes that someone can improve upon my approach. The text is right (white like default, but you can change that). The background gradient and opacity are not perfect (too light??)--some tweaking here might be needed.

    Get the sectionheaderbackground.png here: http://img5.imageshack.us/img5/6616/sectionheaderbackground.png

    // Create a stretchable image that emulates the default gradient
    UIImage *buttonImageNormal = [UIImage imageNamed:@"sectionheaderbackground.png"];
    UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    
    // Create the view for the header
    CGRect sectionFrame = CGRectMake(0.0, 0.0, 320.0, 22.0);
    UIView *sectionView = [[UIView alloc] initWithFrame:sectionFrame];
    sectionView.alpha = 0.9;
    sectionView.backgroundColor = [UIColor colorWithPatternImage:stretchableButtonImageNormal];
    
    // Create the label
    CGRect labelFrame = CGRectMake(10.0, 0.0, 310.0, 22.0);
    UILabel *sectionLabel = [[UILabel alloc] initWithFrame:labelFrame];
    sectionLabel.text = @"Test section label";
    sectionLabel.font = [UIFont boldSystemFontOfSize:18.0];
    sectionLabel.textColor = [UIColor whiteColor];
    sectionLabel.shadowColor = [UIColor grayColor];
    sectionLabel.shadowOffset = CGSizeMake(0, 1);
    sectionLabel.backgroundColor = [UIColor clearColor];
    [sectionView addSubview:sectionLabel];
    [sectionLabel release];
    
    // Return the header section view
    return sectionView;
    
    0 讨论(0)
  • 2021-02-04 17:42

    If you want to recreate the default / standard heightForHeaderInSection, use this:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 44;
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)];
    
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 11, tableView.frame.size.width-100, 21)];
    
        label.text = @"Text";
        label.textColor = [UIColor colorWithRed:76.0/255 green:86.0/255 blue:108.0/255 alpha:1];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];
    
        [view addSubview:label];
    
        return view;
    }
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2021-02-04 17:48

    You can try to mimic it using shadowColor and shadowOffset.

    I have done it in black for example using code like the following:

    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(1, 1);
    

    By messing with the colors and shadow combinations, you can mimic the label pretty well.

    0 讨论(0)
提交回复
热议问题