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

22

我不明白为什么第一个节标题没有显示出来。第二个和第三个都正常显示。我怀疑是因为搜索栏的原因。

我尝试了像UISearchBar covered by section header中偏移整个表格的方法,但我没有注意到它被偏移了。

元素层次结构

截图

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

它对我来说运行得很完美,你改变了任何限制或其他吗? - Yohan
没有限制。当我将搜索栏从Storyboard中移出/移到表格上方时,我可以看到标题的空间,但是它是空白的...搜索栏的层次结构是否正确? - jsky
我只是按照这种层次结构设置的。 - Yohan
3个回答

71

1
我讨厌苹果公司这些“漏洞”……之前没问题,为什么只有第一行出现这种情况…… - djleop
1
请确保返回类型为CGFloat,如果返回类型为NSInteger等其他类型,则可能会出现奇怪的错误。 - kanobius

0
你的代码有一个bug。你的头部在那里,但它设置为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

希望这有所帮助。

谢谢。那确实是一个漏洞。但修复它并没有解决问题。 - jsky
请重新对您的代码进行校对。仅在设置任何限制时查看它。实际上,我也在使用相同的代码,它可以正常工作。请检查我答案附带的更新屏幕截图。 - Balram Tiwari

0
在我的情况下,即使委托已经在XIB文件中连接,我仍需要在代码中显式地分配委托。
    tableView.delegate = self
    tableView.dataSource = self

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接