Mat*_*Car 19 hide uitableview tableview
我发现这个教程隐藏了一个静态TableView的一部分:http://code-ninja.org/blog/2012/02/29/ios-quick-tip-programmatically-hiding-sections-of-a-uitableview -with静电细胞/
它工作得很好,但只是在没有修改的情况下,如果我添加一个部分或一行,它就会很糟糕.我是初学者而且我无法修改它,有人可以帮我隐藏多个部分吗?
非常感谢!
Mat*_*Car 43
- (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
}
}
Run Code Online (Sandbox Code Playgroud)
Kei*_*thB 17
我想分享一些我编写的代码来解决这个问题,然后挖掘出很多答案并碰到许多故障.这适用于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
}
}
Run Code Online (Sandbox Code Playgroud)
现在代码的其余部分只调用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()
}
}
Run Code Online (Sandbox Code Playgroud)
我不得不尝试许多不同的值(返回0,0.1,-1,...)以最终获得满意的解决方案(至少在iOS 9.x上).
我希望这有帮助,如果您有建议改进,请告诉我.
| 归档时间: |
|
| 查看次数: |
16322 次 |
| 最近记录: |