ios 当我滚动 tableview 时,如何使 headerView 滚动(不停留在 tableview 的顶部)伴随 UItableViewCell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20854223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can i make headerView scroll (not stay on the top of the tableview ) accompanying with UItableViewCell when i was scrolling tableview
提问by user3113382
in plainsytle tableview , we set headerViews for each section by delegate method
在plainsytle tableview中,我们通过委托方法为每个部分设置headerViews
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
so,when i scrolling up tableview , the first section's headerView is always showing on the top of tableview before the section totally disappear on the tableview , then the second section's headerView will show on the top of tableview instead . so my question is how can i make headerView scroll accompanying with uitableViewCell , just like group style tableview ?
因此,当我向上滚动 tableview 时,第一部分的 headerView 始终显示在 tableview 的顶部,然后该部分在 tableview 上完全消失,然后第二部分的 headerView 将显示在 tableview 的顶部。所以我的问题是如何让 headerView 与 uitableViewCell 一起滚动,就像组样式 tableview 一样?
采纳答案by Hani Ibrahim
subclass the UITableView and override this method
子类化 UITableView 并覆盖此方法
- (BOOL)allowsHeaderViewsToFloat{
return NO;
}
same for footer
页脚相同
- (BOOL)allowsFooterViewToFloat{
return NO;
}
But I think that this is a private API ... You will not be able to submit it to the AppStore
但我认为这是一个私有API......你将无法将其提交到AppStore
If you will upload it to the AppStore; Then you have two other options
如果您将其上传到 AppStore;那么你还有另外两个选择
- Adding a normal cell instead of the section header
- If you have only one section, then you can simply use table header instead of section header
- 添加普通单元格而不是节标题
- 如果您只有一个部分,那么您可以简单地使用表格标题而不是部分标题
回答by Lal Krishna
You can disable scrolling sectionHeader by changing the table property to -
您可以通过将 table 属性更改为 - 来禁用滚动 sectionHeader
UITableViewStyleGrouped
You have to set it on the initialisation of UITableView.
你必须在 UITableView 的初始化时设置它。
- Set from StoryBoard
- 从故事板设置
OR
或者
UITableView* table = [[UITableView alloc]initWithFrame:myFrame style:UITableViewStyleGrouped];
回答by Akshay Phulare
Just change style of table from "Plain" to "Grouped".
只需将表格样式从“普通”更改为“分组”即可。
回答by iCodeAtApple
I had the same issue and when i browsed i came to this link See The accepted answerfrom and I came to conclusion that you need to set tableViewHeader Style to Group and it works Charm.
我遇到了同样的问题,当我浏览时,我来到了这个链接,请参阅接受的答案,我得出的结论是,您需要将 tableViewHeader Style 设置为 Group 并且它可以正常工作。
回答by Yohan
if you ve more than one section, you can customize it using some thirdt party API
如果您有多个部分,则可以使用某些第三方 API 对其进行自定义
else you can try this using Grouped table instead of plain tableView.
否则你可以尝试使用分组表而不是普通的 tableView。
Try like this when you assign custom view as tableView Header view it becomes header view for table and it will scroll with the table view
当您将自定义视图指定为 tableView Header 视图时,请尝试这样做
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *sectionView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
tableView.tableHeaderView =sectionView;
return sectionView;
}
回答by Mohamed Ramadan
Change the UITableViewStyle
of the table from UITableViewStylePlain
to UITableViewStyleGrouped
, this will make the header scroll up with cells.
UITableViewStyle
将表格的更改UITableViewStylePlain
为 UITableViewStyleGrouped
,这将使标题与单元格一起向上滚动。
回答by Otto
Swift:
迅速:
As Hani Ibrahim pointed out you need to subclass UITableView. Apple says you :
must specify style at creation.
正如 Hani Ibrahim 指出的,你需要继承 UITableView。苹果说你:
must specify style at creation.
So:
所以:
override init(frame: CGRect, style: UITableViewStyle) {
super.init(frame: frame, style: .grouped)
Notice how my super.init uses .grouped as the style. For this style of tableview (.grouped) Apple also says:
注意我的 super.init 如何使用 .grouped 作为样式。对于这种风格的 tableview (.grouped) Apple 还说:
Summary
A table view whose sections present distinct groups of rows. The section headers and footers do not float.
Then you can call the following tableView delegate methods:
viewForHeaderInSection
and heightForHeaderInSection
(notice those are for headers only, use the footer counterparts if you want footers too)
然后您可以调用以下 tableView 委托方法:
viewForHeaderInSection
和heightForHeaderInSection
(注意这些仅用于页眉,如果您也需要页脚,请使用页脚对应方法)
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerLabel = UILabel()
headerLabel.text = "My cool header title"
headerLabel.textAlignment = .center
headerLabel.font = UIFont(name: "OpenSans-Bold", size: 16)
return headerLabel
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
And that should do it! Good luck.
那应该这样做!祝你好运。
回答by Charles McD
You can change the style of the tableView.
您可以更改 tableView 的样式。
let tableView = UITableView(frame: .zero, style: .grouped)