[英]How to show and hide Custom UITableView Header Cell of Table View in Swift3 iOS
I am developing an application in Swift3 where I have to show and hide UITableView Header for different users. 我正在Swift3中开发一个应用程序,其中我必须为不同的用户显示和隐藏UITableView Header。 For displaying
UITableView Header View
, I have created a custom class CustomHeaderCell
of UITableViewCell
. 为了显示
UITableView Header View
,我创建了UITableViewCell
的自定义类CustomHeaderCell
。
Here is my code: 这是我的代码:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 235.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! CustomHeaderCell
return headerCell
}
Now Can anyone please help me to hide this Header of my UITableView
? 现在,有人可以帮助我隐藏
UITableView
此标头吗?
Note: I tried using this tableView.tableHeaderView?.isHidden = true
, but not working. 注意:我尝试使用此
tableView.tableHeaderView?.isHidden = true
,但不起作用。 Should I need to do the validation in heightForHeaderInSection
? 我是否需要在
heightForHeaderInSection
进行验证?
Reference Link to Add HeaderViewCell: http://www.accella.net/knowledgebase/custom-header-and-footer-views-for-uitableviews/ 添加HeaderViewCell的参考链接: http ://www.accella.net/knowledgebase/custom-header-and-footer-views-for-uitableviews/
If you have a way to differentiate users then you can just change the header height like this 如果您有区分用户的方法,则可以像这样更改标题高度
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if userA {
return 235.0
} else {
return 0
}
}
That should help in hiding the header 那应该有助于隐藏标题
You are mixing tableHeaderView
and section header views, which are differents: 您正在混合
tableHeaderView
和section标头视图,这是不同的:
tableHeaderView
is a view showed as a header for the whole UITableView tableHeaderView
是显示为整个UITableView的标题的视图 In your case, you want to use section header views, so you should return empty ones for non concerned users (I assume here sectionNeedHeader
will be replaced by your condition). 在您的情况下,您想使用节标题视图,因此您应该为不相关的用户返回空标题视图(我假设在这里
sectionNeedHeader
将被您的条件替换)。
Also, you better use UITableViewHeaderFooterView
instead of UITableViewCell
. 另外,最好使用
UITableViewHeaderFooterView
而不是UITableViewCell
。 The behavior is globally the same but it's made for this usage: 行为在全局上是相同的,但是是为此使用的:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if sectionNeedHeader {
return 235.0
}
return 0.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if sectionNeedHeader {
let headerCell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderView") as! CustomHeaderView
return headerView
}
return nil
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.