为什么表格中的第一节标题没有显示?

时间:2014-01-13 10:02:53

标签: ios uitableview

我无法弄清楚为什么第一节标题没有显示。第二和第三显示罚款。我怀疑这是因为搜索栏。

我尝试像UISearchBar covered by section header一样抵消整个表格,但我没有注意到它的偏移。

hierarchy of elements

screenshot

- (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];
}

3 个答案:

答案 0 :(得分:59)

没有显示,因为我没有实现 heightForHeaderInSection
更多细节在这里: in iOS 7 viewForHeaderInSection section is starting from 1 not from 0

答案 1 :(得分:0)

您的代码中存在错误。您的标题位于此处,但设置为y = -60,高度为60。

替换:

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

使用此

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

这是此处的屏幕截图

Section 0

Section 0 and 1

希望有所帮助。

答案 2 :(得分:0)

就我而言,即使XIB文件中已经连接了委托,我也需要在代码中显式分配委托。

    tableView.delegate = self
    tableView.dataSource = self