How to use UITableViewHeaderFooterView?

后端 未结 9 2015
傲寒
傲寒 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条回答
  • 2020-12-23 02:45

    Using the new iOS 6 feature of reusable header/footer views involves two steps. You seem to be doing only the first step.

    First step: you're telling the table view what class to use for the section header view, by registering your custom subclass of UITableViewHeaderFooterView (I assume your M3CHeaderFooter is a subclass of UITableViewHeaderFooterView).

    Second step: Tell the table view what view to use (AND reuse) for a header section by implementing the tableView delegate method

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

    So in your viewDidLoad you'd implement something like this:

        // ****** Do Step One ******
        [_tableView registerClass:[M3CHeaderFooter class] forHeaderFooterViewReuseIdentifier:@"TableViewSectionHeaderViewIdentifier"];
    

    Then you'd implement the table View delegate method in the class where you're creating and displaying your table view:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 40.0;
    }
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        static NSString *headerReuseIdentifier = @"TableViewSectionHeaderViewIdentifier";
    
        // ****** Do Step Two *********
        M3CHeaderFooter *sectionHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReuseIdentifier];
       // Display specific header title
       sectionHeaderView.textLabel.text = @"specific title";   
    
        return sectionHeaderView;    
    }
    

    Now mind you that you do not need to subclass UITableViewHeaderFooterView in order to use it. Before iOS 6, if you wanted to have a header view for a section, you'd implement the above tableView delegate method and tell the table view what view to use for each section. So each section had a different instance of a UIView which you provided. This means that if your tableView had 100 sections, and inside the delegate method you created a new instance of a UIView, then you would have given the tableView 100 UIViews for the 100 section headers that were displayed.

    Using the new feature of reusable header/footer views, you create an instance of a UITableViewHeaderFooterView and the system reuses it for each displayed section header.

    If you wanted to have a reusable UITableViewHeaderFooterView without subclassing then you simply change your viewDidLoad to this:

    // Register the class for a header view reuse.
    [_buttomTableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"TableViewSectionHeaderViewIdentifier"];
    

    and then your delegate method to this:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 40.0;
    }
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        static NSString *headerReuseIdentifier = @"TableViewSectionHeaderViewIdentifier";
    
       // Reuse the instance that was created in viewDidLoad, or make a new one if not enough.
        UITableViewHeaderFooterView *sectionHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReuseIdentifier];
        sectionHeaderView.textLabel.text = @"Non subclassed header";
    
        return sectionHeaderView;
    
    }
    

    I hope that was clear enough.

    EDIT: When subclassing the header view, you can implement code similar to the following if you wish to add a custom view to the headerView:

            // Add any optional custom views of your own
        UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 30.0)];
        [customView setBackgroundColor:[UIColor blueColor]];
    
        [sectionHeaderView.contentView addSubview:customView];
    

    Doing this in the subclass, as opposed to viewForHeaderInSection: delegate method (as noted below by Matthias), will ensure that only one instance of any subviews are created. You can then add any properties within the subclass that will allow you to access your custom subview.

    0 讨论(0)
  • 2020-12-23 02:45

    I can't comment under Cameron Lowell Palmer post but to answer Christopher King, there is a simple way to ensure the re-use without sub-classing UITableViewHeaderFooterView and yet still using custom subviews.

    First, do NOT register the class for a header view reuse.

    Then in tableView:viewForHeaderInSection: you simply have to create UITableViewHeaderFooterView when needed:

    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        static NSString *kYourTableViewReusableHeaderIdentifier = @"ID";
    
        UILabel *titleLabel = nil;
    
        UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kYourTableViewReusableHeaderIdentifier];
    
        if (headerView == nil) {
    
            headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:kYourTableViewReusableHeaderIdentifier];
    
            titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(...)];
            titleLabel.tag = 1;
            // ... setup titleLabel 
    
            [headerView.contentView addSubview:titleLabel];
        } else {
            // headerView is REUSED
            titleLabel = (UILabel *)[headerView.contentView viewWithTag:1];
        }
    
        NSString *sectionTitle = (...); // Fetch value for current section
        if (sectionTitle == nil) {
            sectionTitle = @"Missing Title";
        }
    
        titleLabel.text = sectionTitle;
    
        return headerView;
    }
    
    0 讨论(0)
  • 2020-12-23 02:50

    In case it gets lost in the thorough answers above, the thing that people are likely missing (compared to the standard cellForRowAtIndexPath: method) is that you must register the class used for the section header.

    [tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"SectionHeader"];
    

    Try adding registerClass:forHeaderFooterViewReuseIdentifier: and see if it starts working.

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