UITableView:从空部分隐藏标题

Seb*_*ger 73 objective-c uitableview tableview ios

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

我的问题是空部分的标题.有没有办法隐藏它们?数据从coredata加载.

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

TitleForHeader

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

}
Run Code Online (Sandbox Code Playgroud)

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;

}

}
Run Code Online (Sandbox Code Playgroud)

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;
}
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 塞巴斯蒂安

DBD*_*DBD 134

您必须tableView:heightForHeaderInSection:为相应的部分设置为0.最近这种情况发生了变化,让我在几个地方.从UITableViewDelegate它说......

在iOS 5.0之前,对于tableView:viewForHeaderInSection:返回nil视图的节,表视图会自动将标题的高度调整为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
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 静态表有点不同.此代码用于自定义表标题视图,而不仅仅是"标题".如果要设置`title`,则需要在适当的时候为方法调用`tableView:titleForHeaderInSection:`返回`nil`.如果您使用自定义标题视图,则必须覆盖`viewForHeaderInSection`.如果你这样做,这个代码应该工作.如果没有,我需要更多信息来帮助解决这个问题. (3认同)

Lui*_*oza 56

如果什么- tableView:viewForHeaderInSection:return nil如果区段计数为0.

编辑:您可以numberOfRowsInSection用来获取该部分中的元素数量.

编辑:titleForHeaderInSection如果numberOfRowsInSection是0,你也应该返回nil .

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

编辑: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
}
Run Code Online (Sandbox Code Playgroud)

  • 现在这是一个老问题,但是titleForHeaderInSection中的return nil对我来说已经足够了(在检查了该部分的count> 0之后) (2认同)

djd*_*nce 54

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

viewForHeaderInSection - > nil

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

heightForHeaderInSection - > 0.01(不为零!)

 heightForFooterInSection - > 0.01

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

  • 我只是将headerHeight设置为0.01. (10认同)

Six*_*tto 10

看看这个方法-[UITableViewDelegate tableView:heightForHeaderInSection:].特别是其文档附带的注释:

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

  • 遗憾的是它不起作用..我用新代码和新截图更新了问题. (2认同)

gav*_*gav 9

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

因此,根据说法并建立在DBD的答案上,您可以将titleForHeaderInSection:没有行的部分设置为nil,如下所示:

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


Ron*_*oh1 7

在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;
    }
}
Run Code Online (Sandbox Code Playgroud)


And*_*lla 5

这似乎是正确的方式,它将正确的动画和干净的工作......正如Apple所预期的那样......

为tableView委托提供适当的信息

当部分中没有项目时,返回0.0f in:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

..还返回零:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

为tableView执行适当的数据删除

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