【问题标题】:Hide sections of a Static TableView隐藏静态 TableView 的部分
【发布时间】:2013-07-19 16:06:26
【问题描述】:

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

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

非常感谢!

【问题讨论】:

  • 尝试使静态表动态化的目的是什么?这就是动态表的用途。
  • 我的想法是在我更复杂的应用程序中实现它,但我无法将所有内容都转换为动态,但我(或多或少)解决了!

标签: uitableview hide tableview


【解决方案1】:
- (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 
    }    
}

【讨论】:

  • 如果表是组表,那么最好为页眉/页脚高度返回 0.1。返回 0 会导致 UITableView 在该部分应该存在的位置仍然存在间隙。
  • 这是个好主意,通常我返回 1 但 0.1 更好! =)
【解决方案2】:

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

我的用例是我想使用故事板静态分组 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 上)。

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

【讨论】:

  • 我创建了 UITableViewController 的一个子类,称为 DynamicUITableViewController。它允许隐藏部分/行而无需在 TableViewControllers 中实现所有这些代码。这是 github 仓库:(github.com/tkeithblack/DynamicUITableViewController)
  • 我刚刚发现,当您不希望缩小行或标题时,如果您调用 super.tableView(...),而不是返回 -1,这将遵循自定义行高,即设置在故事板中。我已经编辑了上面的示例和我的 github 条目以反映此更改。
  • table​View(_:​will​Display​Header​View:​for​Section:​)table​View(_:​will​Display​Footer​View:​for​Section:​) 中,您可以简单地执行view.isHidden = true,而不是强制转换并使UILabel 透明。
  • 加里,感谢您的建议。我试过这个,虽然它确实隐藏了不需要的标题文本,但它也隐藏了一些我不想隐藏的不相关的标题。我的猜测是,这可能与细胞的可重复使用特性有关。为什么 isHidden 是这种情况而不是 textColor=clear 我不确定,但可能在每次调用绘制标题之前系统都会重置文本颜色而 isHidden 不是。
  • 试图实现这个,我应该在哪里使用 shouldHideSection(section: x)
【解决方案3】:

对于斯威夫特

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

【讨论】:

  • 节省了我该死的时间!谢谢你。 ?
【解决方案4】:

如果您返回 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;
}

【讨论】:

    【解决方案5】:

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

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-22
        • 1970-01-01
        • 1970-01-01
        • 2010-11-07
        • 1970-01-01
        相关资源
        最近更新 更多