Uitableview 隐藏静态表视图的部分

Uitableview 隐藏静态表视图的部分,uitableview,hide,tableview,Uitableview,Hide,Tableview,我发现本教程隐藏了静态TableView的一部分: 它工作得很好,但只有在不修改它的情况下,如果我添加一个节或一行,它工作得很差。我是一个初学者,我不能修改它,有人能帮我隐藏多个部分吗 非常感谢你 -(CGFloat)tableView:(UITableView*)headerinsection:(NSInteger)section{ 如果(节==2&&U hideTableSection){ //选定节段的收割台高度 返回0.1; }否则{ //保持所有其他标题不变 返回[super tabl

我发现本教程隐藏了静态TableView的一部分:

它工作得很好,但只有在不修改它的情况下,如果我添加一个节或一行,它工作得很差。我是一个初学者,我不能修改它,有人能帮我隐藏多个部分吗

非常感谢你

-(CGFloat)tableView:(UITableView*)headerinsection:(NSInteger)section{
如果(节==2&&U hideTableSection){
//选定节段的收割台高度
返回0.1;
}否则{
//保持所有其他标题不变
返回[super tableView:tableView heightForHeaderInSection:section];
}  
}
-(CGFloat)表格视图:(UITableView*)表格视图页脚高度:(NSInteger)节{
如果(节==2&&U hideTableSection){
//选定节段的收割台高度
返回0.1;
}否则{
//保持所有其他页脚不变
返回[超级表视图:表视图页脚高度:节];
} 
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
如果(节==1){//感兴趣节的索引号
if(隐藏部分){
返回0;//单击“隐藏”时节中的行数
}否则{
返回2;//单击“显示”时节中的行数(若大于情节提要中的行数,则应用程序将崩溃)
}
}否则{
return[super tableView:tableView numberofrowsinssection:section];//不删除所有其他行
}    
}

将节值设置为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;
}

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

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

  return 44;
}
还实现了header的视图,并为不想显示的部分返回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;
}

我想与大家分享一些我为解决这个问题而编写的代码,这些代码是在挖掘了大量答案并遇到了许多小故障之后编写的。这是针对xCode 7.2.1的。(Swift中的代码示例)

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

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

func应该定义节(节:Int)->Bool{
开关段{
案例0://根据以下条件隐藏此部分
返回用户!.isProvider()?false:true
案例2:
返回someLogicForHiddingSectionThree()?false:true
违约:
返回错误
}
}
现在代码的其余部分只调用shouldHideSection()

//隐藏标题
覆盖func表格视图(表格视图:UITableView,HeaderInSection部分的高度:Int)->CGFloat{
返回值应为中间截面(截面)?0.1:super.表视图(表视图,头部高度截面:截面)
}
//隐藏页脚
重写func tableView(tableView:UITableView,heightforfooterInstruction节:Int)->CGFloat{
return shoulldhidesection(section)?0.1:super.tableView(tableView,heightforfooter位置:section)
}
//隐藏隐藏部分中的行
重写func tableView(tableView:UITableView,heightForRowAtIndexPath:nsindepath)->CGFloat{
返回shouldHideSection(indexPath.section)?0:super.tableView(tableView,heightForRowAtIndexPath:indexPath)
}
//隐藏标题文本,使其清晰
重写func tableView(tableView:UITableView,willDisplayHeaderView:UIView,forSection节:Int){
如果应该,请参阅第节(第节){
让headerView=查看为!UITableViewHeaderFooterView
headerView.textLabel!.textColor=UIColor.clearColor()
}
}
//通过清除隐藏页脚文本
重写func tableView(tableView:UITableView,willDisplayFooterView:UIView,forSection节:Int){
如果应该,请参阅第节(第节){
让footerView=查看为!UITableViewHeaderFooterView
footerView.textLabel!.textColor=UIColor.clearColor()
}
}
我必须用许多不同的值(返回0,0.1,-1,…)进行实验,以最终得到令人满意的解决方案(至少在iOS9.x上)


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

如果您从情节提要中删除章节标题,它将自动消失。我指的不仅仅是标题内容,还有它所占据的空间。

对于Swift

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)
}

使静态表成为动态表的目的是什么?这就是动态表的用途。我的想法是在我更复杂的应用程序中实现它,在那里我无法将所有内容都转换为动态,但我(或多或少)解决了这个问题!如果表格是组表格,则页眉/页脚高度最好返回0.1。返回0会导致UITableView在节应该位于的位置仍有间隙。这是个好主意,通常我返回1,但0.1更好!!=)我创建了一个名为DynamicUITableViewController的UITableViewController子类。它允许隐藏节/行,而不必在TableViewController中实现所有这些代码。这是github repo:()我刚刚发现,如果调用super.tableView(…),您不希望收缩行或标题,那么它将遵循脚本中设置的自定义行高度,而不是返回-1。我编辑了上面的示例和我的github条目以反映此更改