How to use UITableViewHeaderFooterView?

后端 未结 9 2045
傲寒
傲寒 2020-12-23 02:12

Hi I want to use UITableHeaderFooterView in my app and i am doing this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional          


        
9条回答
  •  梦毁少年i
    2020-12-23 02:27

    UITableViewHeaderFooterView is one of the few places I would programmatically handle the view rather than use Storyboard or a XIB. Since you cannot officially use appearance proxy and there is no IB way to do it without abusing UITableViewCells. I do it the old-fashioned way and just use the tag on the label to fetch the custom elements.

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kSectionHeaderReuseIdentifier];
        if (headerView == nil) {
            [tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:kSectionHeaderReuseIdentifier];
            headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kSectionHeaderReuseIdentifier];
        }
    
        UILabel *titleLabel = (UILabel *)[headerView.contentView viewWithTag:1];
        if (titleLabel == nil) {
            UIColor *backgroundColor = [UIColor blackColor];
            headerView.contentView.backgroundColor = backgroundColor;
            titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 0.0f, 300.0f, 44.0f)];
            titleLabel.textColor = [UIColor whiteColor];
            titleLabel.backgroundColor = backgroundColor;
            titleLabel.shadowOffset = CGSizeMake(0.0f, 0.0f);
            titleLabel.tag = 1;
            titleLabel.font = [UIFont systemFontOfSize:24.0f];
            [headerView.contentView addSubview:titleLabel];
        }
    
        NSString *sectionTitle = [self.sections objectAtIndex:section];
        if (sectionTitle == nil) {
            sectionTitle = @"Missing Title";
        }
    
        titleLabel.text = sectionTitle;
    
        return headerView;
    }
    

提交回复
热议问题