29

表格中的某些部分不包含任何数据,并希望隐藏该部分。

这该怎么做?

4

12 回答 12

59

实际上,您可以“隐藏”一个部分。如果您想使用与内置联系人应用程序类似的行为,其中部分隐藏但仍列在右侧的索引中,您可以执行以下操作:

实现UITableViewDataSource协议:

  • 返回方法中的所有部分名称(甚至是隐藏的部分)sectionIndexTitlesForTableView

  • 对于每个空部分,niltitleForHeaderInSection方法返回。

  • 0对于该numberOfRowsInSection方法的每个空部分返回。

我发现这比删除部分效果更好,因为用户具有一致的索引导航。

于 2011-05-15T19:41:08.387 回答
21

您不能像这样“隐藏”一个部分,但您可以使用该deleteSections:withRowAnimation:方法从表视图中“删除”它。这将使用可选动画将其从视图中移除,而不会影响您的支持数据。(但是,无论如何,您都应该更新数据,以免该部分再次出现。)

更多信息:UITableView 类参考

于 2009-06-30T00:47:10.057 回答
14

确实,0 不是页眉和页脚的有效高度。但是,高度是 CGFloat 值。您可以为节页眉和页脚的高度指定一个非常小的数字(我使用了 0.1)。

有点黑客,但它的工作原理。

于 2012-04-16T06:43:36.053 回答
5

我不同意蒂姆。我们有一种方法可以从代码中的任何位置访问表的任何部分/行并更改其 .hidden 属性(以及所有其他属性)。

这是我通常使用的方式:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2];
[self.SeymourCakesTableView cellForRowAtIndexPath:indexPath].hidden = YES;
于 2012-04-12T06:43:12.487 回答
2

您可以将该部分中的行数设置为 0。但是,它将在原来的位置留下一个明显的空白区域。

于 2010-02-01T23:54:14.533 回答
2

您还可以从该 numberofSectionsInTableView: 方法返回包含数据的记录数,并使用 switch(indexPath.section) 让空记录“落入”到下一个开关的位置,例如:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    switch (indexPath.section) {
        case 0:
            return <header cell>;
            break;

        case 1:
            if(firstRecordHasData){
                return <second cell>;
                break;
            }

        case 2:
            if(secondRecordHasData){
                return <second cell>;
                break;
            }

        case 3:
            return <some other cell>;
            break;

        default:
            return <a regular cell>;
            break;
    }   
}

我为此苦苦挣扎了一段时间,因为我不得不在分组表中间省略部分。尝试将单元格、页眉和页脚高度设置为 0.0,但这不起作用。由于调用的方法取决于所选行,因此不能只删除某些部分。如果有多个子例程调用,这将是一个巨大的 if..else if...else if。很高兴我想到了好的旧开关方法,也许它也可以帮助你:-)

于 2010-05-13T14:13:37.123 回答
1

您可能需要从支持您的表的数据中删除该部分本身。我不认为有任何东西可以让你隐藏一个部分。

于 2009-06-30T00:23:38.760 回答
1

您可以将特定的部分行高度设置为 0。此外,如果需要,可以使用部分标题。数据源仍然存在,只是没有出现。

截面行

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (_shouldHidden) {
            return 0.0;
        }
        else {
            return 55.0;
        }
    }
    else {
        return 55.0;
    }
}
于 2014-06-26T14:40:41.640 回答
1

如果您返回 0 作为部分的高度,Apple API 将忽略它。所以只返回一个大于 0 的小值。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  if (section == 0) {
    return 1;
  }

  return 44;
}

还为标题实现视图,并为您不想显示的部分返回 nil。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  if (section == 0 && !self.personaCells.count) {
    return nil;
  }

  UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)];
  UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, headerView.frame.size.width, 20)];
  NSString *headerTitle = @"SAMPLE TITLE";
  headerLabel.text = headerTitle;    
  [headerView addSubview:headerLabel];
  return headerView;
}
于 2015-07-16T17:18:41.663 回答
0

试试这样: -

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat headerHeight=10.f;
    if (section==0)
    {
        headerHeight=0.01f;
    }
    else
    {
        headerHeight=50.0f;
    }
    return headerHeight;
}
于 2015-04-05T01:22:38.883 回答
0

对于静态表格的情况,即在 Storyboard 中配置表格部分和单元格。以下是我根据条件隐藏指定部分的策略。

第一步:实现两个func定义在UITableViewDelegate - heightForRowAt -heightForHeaderInSection

例如,这里是 swift 代码:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
{
   // if indexPath contains the specified section AND
   //    the condition for hiding this section is `true`
   //       return CGFloat(0)
   // else 
   //    return super.tableView(tableView, heightForRowAt: indexPath)
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat 
{
   // similar logic to set header height
}

第二步:定义一个函数来设置隐藏特定部分的单元格,并从 viewWillAppear 中调用它:

private func setSectionVisible()
{
   /*
   if condition for visible is true
      let index = IndexPath(row:..., section:...)
      let cell = self.tableView.cellForRow(at: index)
      cell.isHiden = true
   */
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.setSectionVisible()
}

如果您需要重新加载 tableview,您可能需要setSectionVisible()再次调用。

我认为这种策略可能适用于来自 DataSource 的动态数据。通过这种方式,您可以控制何时使特定部分可见或隐藏。

于 2016-11-27T05:44:29.380 回答
0

为了隐藏一个部分,即使在表格视图的中间,您也需要以下所有内容

#define DEBUG_SECTION 1

#if ! DEBUG_BUILD
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == DEBUG_SECTION)
        return CGFLOAT_MIN;
    return [super tableView:tableView heightForHeaderInSection:section];
}
//------------------------------------------------------------------------------

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == DEBUG_SECTION)
        return CGFLOAT_MIN;
    return [super tableView:tableView heightForFooterInSection:section];
}
//------------------------------------------------------------------------------

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == DEBUG_SECTION)
        return nil;
    return [super tableView:tableView titleForHeaderInSection:section];
}
//------------------------------------------------------------------------------

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    if (section == DEBUG_SECTION)
        return nil;
    return [super tableView:tableView titleForFooterInSection:section];
}
//------------------------------------------------------------------------------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == DEBUG_SECTION)
        return 0;
    return [super tableView:tableView numberOfRowsInSection:section];
}
//------------------------------------------------------------------------------
#endif // #if ! DEBUG_BUILD

如果您遗漏了 titleFor/headerFor...
如果您遗漏了 heightFor... 您将得到一些空行,标题/页脚标题文本仍将出现

于 2017-05-14T20:48:05.240 回答