【问题标题】:How to remove the empty section header in UITableView如何删除 UITableView 中的空白部分标题
【发布时间】:2015-03-04 05:25:00
【问题描述】:

我有一个 UITableView,因为我有三个标题标题。问题是:当我的标题标题值为空时意味着标题不应显示在表格视图中。我尝试了很多方法对我没有帮助。谁能帮帮我。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [reverseOrder1 objectAtIndex:section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
 return 60;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 75)];
UILabel *headerTitile =[[UILabel alloc]initWithFrame:CGRectMake(20, -5, tableView.bounds.size.width-20, 75)];
headerTitile.text = [reverseOrder1 objectAtIndex:section];

headerTitile.textColor = [UIColor whiteColor];
headerTitile.TextAlignment=NSTextAlignmentCenter;
[headerView addSubview:headerTitile];
headerTitile.font = [UIFont fontWithName:@"Arial" size:16.0f];

headerView.backgroundColor = [UIColor colorWithRed:48/255.0f green:119/255.0f blue:21/255.0f alpha:1];

return headerView;

}

【问题讨论】:

  • 可能重复的this 。提问前请先搜索
  • 你的表中有多少个部分?
  • @AbhishekNath 3 部分兄弟
  • @huy nghia 我试过这个链接它没有帮助我
  • [reverseOrder1 objectAtIndex:section] 是 NSString 吗?您想在标题标题为零时删除标题?尝试设置 viewForHeaderInSection: return nil heightForHeaderInSection: = 0 when [reverseOrder1 objectAtIndex:section] nil

标签: ios objective-c uitableview


【解决方案1】:

你可以这样使用

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

    if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section == 0]) 
    {
        return 0;
    } 
    else
     {
        return 60;
     }
    }

【讨论】:

    【解决方案2】:

    除了 Ravi 的回答之外,请确保在情节提要中将 table view 的 section height 属性设置为 0。

    【讨论】:

    • 强制至少为 1