简体   繁体   中英

Why is my first section header in table not showing?

I can't figure out why the first section header isn't showing. The second and third show fine. I suspect it's because of the search bar.

I've tried offsetting the whole table like in UISearchBar covered by section header but I didn't notice it offset.

元素的层次结构

截图

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    // create the parent view that will hold header Label
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0,-60,300,60)];

    // create the label object
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.frame = CGRectMake(0,0,self.view.frame.size.width,60);
    UIColor* mainColor = [UIColor colorWithRed:47.0/255 green:168.0/255 blue:228.0/255 alpha:1.0f];
    headerLabel.backgroundColor = mainColor;
    headerLabel.font = [UIFont boldSystemFontOfSize:18];

    headerLabel.textAlignment = UITextAlignmentCenter;

    if(section == 0)
        headerLabel.text = @"Friends";
    if(section == 1)
        headerLabel.text = @"Second Section Header";
    if(section == 2)
        headerLabel.text = @"Third Section Header";
        headerLabel.textColor = [UIColor whiteColor];

    [customView addSubview:headerLabel];

   return customView;        
}

- (void) hideSearchBar
{
    self.feedTableView.contentOffset = CGPointMake( 0, self.searchBar.frame.size.height );
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //Number of rows it should expect should be based on the section
    NSDictionary *dictionary = [_imageDataArray objectAtIndex:section];
    NSArray *array = [dictionary objectForKey:@"data"];
    return [array count];
}

It wasn't showing because i hadn't implemented heightForHeaderInSection
More details here: in iOS 7 viewForHeaderInSection section is starting from 1 not from 0

You got a bug in your code. Your header is there but it is setting at a y = -60 with a height of 60.

Replace :

// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0,-60,300,60)];

with This

// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,300,60)];

Here is the screen shot here

第0节

第0节和第1节

Hope that helps.

In my case I needed to explicitly assign delegates in code, even though delegates were already connected in the XIB file.

    tableView.delegate = self
    tableView.dataSource = self

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM