【问题标题】:UITableView: hide header from empty sectionUITableView:从空白部分隐藏标题
【发布时间】:2012-04-02 00:24:09
【问题描述】:

我有一个 UITableView,它显示当月的费用(见截图):

我的问题是空部分的标题。有什么办法可以隐藏它们吗? 数据是从 coredata 加载的。

这是生成标题标题的代码:

标题标题

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) {
    return nil;
} else {

NSDate *today = [NSDate date ];
int todayInt = [dataHandler getDayNumber:today].intValue;

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:(-(todayInt-section-1)*60*60*24)];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]];    
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
    return formattedDateString;}

}

ViewForHeader

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) {
    return nil;
} else {

    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 312, 30)];
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(4, 9, 312, 20)];
    UIView *top = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 312, 5)];
    UIView *bottom = [[UIView alloc]initWithFrame:CGRectMake(0, 5, 312, 1)];

    [top setBackgroundColor:[UIColor lightGrayColor]];
    [bottom setBackgroundColor:[UIColor lightGrayColor]];

    [title setText:[expenseTable.dataSource tableView:tableView titleForHeaderInSection:section]];
    [title setTextColor:[UIColor darkGrayColor]];
    UIFont *fontName = [UIFont fontWithName:@"Cochin-Bold" size:15.0];
    [title setFont:fontName];


    [headerView addSubview:title];
    [headerView addSubview:top];
    [headerView addSubview:bottom];

    return headerView;

}

}

heightForHeader

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

NSLog(@"Height: %d",[tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0);
if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section == 0]) {
    return 0;
} else {
    return 30;
}
}

numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {

int rows = 0;
for (Expense* exp in [dataHandler allMonthExpenses]) {
    if ([exp day].intValue == section) {
        rows++;
    }
}

return rows;
}

塞巴斯蒂安

【问题讨论】:

    标签: objective-c ios uitableview tableview


    【解决方案1】:

    对于相应的部分,您必须将 tableView:heightForHeaderInSection: 设置为 0。这是最近发生的变化,让我在几个地方。来自UITableViewDelegate 它说...

    在 iOS 5.0 之前,表格视图会自动调整高度 tableView:viewForHeaderInSection: 部分的标题为 0 返回零视图。在 iOS 5.0 及更高版本中,您必须返回实际的 此方法中每个节标题的高度。

    所以你必须做类似的事情

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) {
            return 0;
        } else {
            // whatever height you'd want for a real section header
        }
    }
    

    【讨论】:

    • 很遗憾它不起作用..我用新代码和新屏幕截图更新了这个问题。
    • 静态表有点不同。此代码用于自定义表头视图,而不仅仅是 title。如果您要设置title,则需要在适当的时候为方法调用tableView:titleForHeaderInSection: 返回nil。如果您使用自定义标题视图,则必须覆盖 viewForHeaderInSection。如果你这样做,这段代码应该可以工作。如果没有,我需要更多信息来帮助解决这个问题。
    • 太棒了...非常感谢。早些时候我返回一个空白UIView 像这样:else{vw = [[UIView alloc]init];[vw setHidden:YES];}
    • 我在上面的代码 + 部分中使用了 viewforheader,并且在 ios 6 甚至更低版本中都能完美运行。 thnnxx 伙计
    • 为我工作 - 我在 ios6 上
    【解决方案2】:

    如果在 - tableView:viewForHeaderInSection:return nil 如果节数为 0 会怎样。

    编辑: 您可以使用numberOfRowsInSection 来获取section 中的元素个数。

    编辑: 如果numberOfRowsInSection 为0,您可能也应该在titleForHeaderInSection 中返回nil。

    编辑: 你实现了以下方法吗?

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    

    编辑Swift 3示例

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        switch section {
        case 0:
            if self.tableView(tableView, numberOfRowsInSection: section) > 0 {
                return "Title example for section 1"
            }
        case 1:
            if self.tableView(tableView, numberOfRowsInSection: section) > 0 {
                return "Title example for section 2"
            }
        default:
            return nil // when return nil no header will be shown
        }
        return nil
    }
    

    【讨论】:

    • 很遗憾它不起作用..我用新代码和新屏幕截图更新了这个问题。
    • 没有错误。刚刚完成编辑问题。现在新代码和新屏幕截图已经出现。
    • 对了,你实现了 numberOfRowsInSection 吗?
    • 如果您检查笔尖中节标题的大小怎么办?..也许如果您使用 0,您会得到更好的结果..因为您是在代码中设置它。
    • 这是一个老问题了,但是titleForHeaderInSection 中的return nil; 对我来说已经足够了(当然,在检查了count > 0 的那个部分之后)
    【解决方案3】:

    在我奇怪的情况下我必须返回:

    viewForHeaderInSection -> 无

     viewForFooterInSection -> nil (不要忘记页脚!)

    heightForHeaderInSection -> 0.01(不是零!)

     heightForFooterInSection -> 0.01

    只有在这种情况下,空白部分才会完全消失

    【讨论】:

    • 我只是将 headerHeight 设置为 0.01。
    • iOS 8.4 我仍然可以看到使用 0.01 的文本,但 0.0 的魅力覆盖 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { if sectionedTableData[section].count == 0 { 返回 CGFloat(0.0) } 返回 CGFloat(40.0) }
    • 这非常适合我。不幸的是,我在其他任何地方都找不到这个。谢谢老兄。
    • 即使在 iOS 12.1 中,您仍然需要将高度设置为0.01.... iOS 有一些奇怪的错误。
    【解决方案4】:

    看看方法-[UITableViewDelegate tableView:heightForHeaderInSection:]。尤其是文档随附的注释:

    在 iOS 5.0 之前,表格视图会自动调整高度 对于 tableView:viewForHeaderInSection: 的部分,标题为 0 返回nil 视图。在 iOS 5.0 及更高版本中,您必须返回实际的 此方法中每个节标题的高度。

    【讨论】:

    • 非常感谢 mch 伙计们 - 我会在几个小时内更新您的信息,并且会接受并支持任何最有效的方法 :-)
    • 很遗憾它不起作用..我用新代码和新屏幕截图更新了这个问题。
    【解决方案5】:

    我知道这是一个老问题,但我想补充一下。我更喜欢将 titleHeader 设置为 nil 而不是将 heightForHeaderInSection 更改为 0 的方法,因为它可能会导致 indexPath 的问题是 +1 应该是由于标题仍然存在但隐藏。

    因此,在DBD's answer 的基础上,您可以将没有行的部分的titleForHeaderInSection: 设置为nil,如下所示:

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) {
            return nil;
        } else {
            // return your normal return
        }
    }
    

    【讨论】:

      【解决方案6】:

      2015 年使用 iOS 8 和 Xcode 6,以下对我有用:

      /* Return the title for each section if and only if the row count for each section is not 0. */
      
      -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
      
          if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) {
              return nil;
          }else{
      
          // here you want to return the title or whatever string you want to return for the section you want to display
      
          return (SomeObject*)someobjectArray[section].title;
          }
      }
      

      【讨论】:

        【解决方案7】:

        这似乎是正确的方法,它会正确地制作动画并且工作起来很干净......正如 Apple 所期望的......

        向 tableView 委托提供适当的信息

        当section中没有item时,返回0.0f in:

        -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
        

        ..同样返回 nil 为:

        -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
        

        对 tableView 进行适当的数据删除

        1. 致电[tableView beginUpdates];
        2. 从您的数据源中删除项目,跟踪元素被删除的位置..
        3. 使用您删除的单元格的 indexPaths 调用 deleteRowsAtIndexPaths
        4. 如果您的数据源中没有任何项目(此处您将只得到标题)。致电reloadSections: 重新加载该部分。这将触发正确的动画并隐藏/滑动/淡入标题。
        5. 最后调用[tableView endUpdates];完成更新..

        【讨论】:

        • 感谢有关 reloadSections 的提示::-) 仍然无法使用 iOS11 为我工作。有点烦人:/
        • 为什么我们必须打电话给reloadSections?我注意到,如果我不调用它,我的表格视图标题会重叠(直到我滚动,才会显示正确的标题)。
        【解决方案8】:

        斯威夫特 4.2

        将 heightForHeaderInSection 设置为零,如果您有自定义剖面视图,请将其设置为 nil 以用于没有单元格的部分。

        func tableView(_ tableView: UITableView,
                           heightForHeaderInSection section: Int) -> CGFloat {
                return height_DefaultSection
            }
        
        func tableView(_ tableView: UITableView,
                           viewForHeaderInSection section: Int) -> UIView? {
        
                return tableView.dataSource?.tableView(tableView, numberOfRowsInSection: section) == 0 ? nil: headerView(tableView: tableView, section: section)
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多