24

我发现本教程隐藏了静态 TableView 的一部分:http://code-ninja.org/blog/2012/02/29/ios-quick-tip-programmatically-hiding-sections-of-a-uitableview - 带有静态单元格/

它工作得很好,但只有在不修改它的情况下,如果我添加一个部分或一行,它就会很糟糕。我是初学者,我无法修改它,有人可以帮我隐藏多个部分吗?

太感谢了!

4

6 回答 6

46
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {            
    if (section == 2 && _hideTableSection) {
        //header height for selected section
        return 0.1; 
    } else {
        //keeps all other Headers unaltered 
        return [super tableView:tableView heightForHeaderInSection:section]; 
    }  
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {        
    if (section == 2 && _hideTableSection) {
        //header height for selected section
        return 0.1; 
    } else {
        // keeps all other footers unaltered
        return [super tableView:tableView heightForFooterInSection:section]; 
    } 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) { //Index number of interested section
        if (hideTableSection) {
            return 0; //number of row in section when you click on hide
        } else {
            return 2; //number of row in section when you click on show (if it's higher than rows in Storyboard, app will crash)
        }
    } else {
        return [super tableView:tableView numberOfRowsInSection:section]; //keeps inalterate all other rows 
    }    
}
于 2013-07-20T16:29:50.377 回答
18

在挖掘了很多答案并遇到了许多故障后,我想分享一些我为解决这个问题而编写的代码。这适用于 xCode 7.2.1。(Swift 中的代码示例)

我的用例是我想使用 Storyboard 静态分组 TableView 的易用性,但我需要根据用户配置文件隐藏特定部分。为了使这项工作(如其他帖子中所述),我需要隐藏页眉和页脚、部分中的行并隐藏页眉/页脚文本(至少在顶部)。我发现如果我不隐藏(使透明)文本,那么用户可以向上滚动到表格顶部(在导航控制器下)并看到所有文本都挤在一起。

我想让它易于修改并且不希望条件在我的代码中传播,所以我创建了一个名为 shouldHideSection(section: Int) 的函数,这是我必须更改以修改隐藏哪些行的唯一函数。

func shouldHideSection(section: Int) -> Bool {
    switch section {
    case 0:  // Hide this section based on condition below
        return user!.isProvider() ? false : true

    case 2:
        return someLogicForHiddingSectionThree() ? false : true

    default:
        return false
    }
}

现在剩下的代码只调用shouldHideSection()。

// Hide Header(s)
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return shouldHideSection(section) ? 0.1 : super.tableView(tableView, heightForHeaderInSection: section)
}

// Hide footer(s)
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return shouldHideSection(section) ? 0.1 : super.tableView(tableView, heightForFooterInSection: section)
}

// Hide rows in hidden sections
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return shouldHideSection(indexPath.section) ? 0 : super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}

// Hide header text by making clear
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if shouldHideSection(section) {
        let headerView = view as! UITableViewHeaderFooterView
        headerView.textLabel!.textColor = UIColor.clearColor()
    }
}

// Hide footer text by making clear
override func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
    if shouldHideSection(section) {
        let footerView = view as! UITableViewHeaderFooterView
        footerView.textLabel!.textColor = UIColor.clearColor()
    }
}

我必须尝试许多不同的值(返回 0、0.1、-1、...)才能最终获得令人满意的解决方案(至少在 iOS 9.x 上)。

我希望这对您有所帮助,如果您提出改进建议,请告诉我。

于 2016-03-15T05:09:06.987 回答
4

对于斯威夫特

var hideTableSection = true

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    
    if section == 2 && hideTableSection {
        //header height for selected section
        return 0.1
    }
    
    //keeps all other Headers unaltered 
    return super.tableView(tableView, heightForHeaderInSection: section)
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    
    if section == 2 && hideTableSection {
        //header height for selected section                
        return 0.1
    }
    
    return super.tableView(tableView, heightForFooterInSection: section)
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    if section == 1 { //Index number of interested section
        if hideTableSection {
            return 0 //number of row in section when you click on hide
        } else {
            return 2 //number of row in section when you click on show (if it's higher than rows in Storyboard, app will crash)
        }
    } else {
        return super.tableView(tableView, numberOfRowsInSection: section) //keeps inalterate all other rows 
    }
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    if section == 2 && hideTableSection {
        return ""
    }

    return super.tableView(tableView, titleForHeaderInSection: section)
}

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {

    if section == 2 && hideTableSection {
        return ""
    }

    return super.tableView(tableView, titleForFooterInSection: section)
}
于 2016-12-29T23:51:16.523 回答
2

如果您返回 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:20:27.887 回答
1

将部分值设置为 0.01,无论您想隐藏什么部分,都可以这样尝试:-

- (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:21:09.860 回答
0

如果您从情节提要中删除节标题的标题,它会自动消失。我的意思不仅是标题内容,还包括它所占用的空间。

于 2016-05-03T15:29:40.220 回答