uitableview headerViewForSection: always returns nil

前端 未结 3 1840
故里飘歌
故里飘歌 2020-12-09 17:10

I have a table with custom header views that no matter when, or what value I choose for section, I always get nil value. I have another table with the same problem.

相关标签:
3条回答
  • 2020-12-09 17:29

    Answer

    From the docs:

    To make the table view aware of your header or footer view, you need to register it. You do this using the registerNib:forHeaderFooterViewReuseIdentifier: or registerClass:forHeaderFooterViewReuseIdentifier: method of UITableView.

    (The Swift equivalent is register(_:forHeaderFooterViewReuseIdentifier:).)

    So you need to register the nib, and then get it using a reuse identifier, instead of pulling it straight out of the app bundle, which is what you're doing now.

    ...if you want to use the headerViewForSection method.

    Alternate Answer

    Alternatively, you could check whether to keep spinning inside the viewForHeaderInSection method and then send just call:

    [self.tableView beginUpdates];
    [self.tableView endUpdates];
    

    To refresh the section header.

    (Note that this alternative approach will destroy and recreate your whole view, so if you have a big table with lots of data, it may not be very efficient.)

    0 讨论(0)
  • 2020-12-09 17:33

    It's been awhile since this question was asked and recently I had come across a similar issue and had asked my own question here: UITableView -headerViewForSection returns (null)
    I believe I have the answer.


    Prerequisite Steps:

    1. Create a UITableViewHeaderFooterView subclass and name it CustomHeaderView
    2. Create a view interface nib file for the class (apparently you have named it iPadTableCells here)
    3. In the xib, select the View & in it's Identity Inspector
      • Specify the Custom Class as CustomHeaderView
    4. Make a property, synthesize and connect it to the xib
      • @property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;

    Use the following code:

    - (UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        static NSString *HeaderIdentifier = @"header";
        CustomHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    
        if(!header) {
            NSArray* objs = [[NSBundle mainBundle] loadNibNamed:@"iPadTableCells"
                                                          owner:nil
                                                        options:nil];
            header = [objs objectAtIndex: 0];
        }
    
        [header.activityIndicator startAnimating];
        return header;
    }
    

    then you can access it this way:

    CustomHeaderView *headerView = (CustomHeaderView*)[tableView headerViewForSection:section];
    [headerView.activityIndicator stopAnimating];
    
    0 讨论(0)
  • 2020-12-09 17:52

    In swift

    All you need is to create an instance of UITableViewHeaderFooterView for the header view you need to return

     func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let sectionHeaderView = UITableViewHeaderFooterView()
            //customize your view here
            return sectionHeaderView
        }
    
    0 讨论(0)
提交回复
热议问题