【问题标题】:Why is my first section header in table not showing?为什么我的表中的第一节标题没有显示?
【发布时间】:2014-01-13 10:02:52
【问题描述】:

我不明白为什么第一节标题没有显示。第二和第三显示很好。我怀疑这是因为搜索栏。

我试过像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];
}

【问题讨论】:

  • 它对我来说非常完美,您是否更改了任何约束或任何约束?
  • 无限制。当我将搜索栏从情节提要的表格中移出/上方时,我可以看到标题的空间,但它是空白的...搜索栏层次结构是否正常?
  • 是的,我只像这样设置层次结构

标签: ios uitableview


【解决方案1】:

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

【讨论】:

  • 我讨厌苹果的这些“错误”...之前没有问题,然后,为什么只针对第一行...
  • 并确保返回类型为 CGFloat。例如,如果返回类型是 NSInteger,则会出现奇怪的错误。
【解决方案2】:

您的代码中有错误。您的标题在那里,但它设置为 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)];

这里是屏幕截图

希望对您有所帮助。

【讨论】:

  • 谢谢。那确实是一个错误。但修复它并没有解决问题。
  • 请重新校对您的代码。如果您设置了任何约束,请查看它。事实上,我也在使用相同的代码,它工作正常。请检查我的答案所附的更新屏幕。
【解决方案3】:

在我的例子中,我需要在代码中明确分配委托,即使委托已经在 XIB 文件中连接。

    tableView.delegate = self
    tableView.dataSource = self

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 2023-01-25
    • 2019-04-28
    • 2016-04-11
    相关资源
    最近更新 更多