简体   繁体   中英

how can i make headerView scroll (not stay on the top of the tableview ) accompanying with UItableViewCell when i was scrolling tableview

in plainsytle tableview , we set headerViews for each section by delegate method

-(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 ?

You can disable scrolling sectionHeader by changing the table property to -

UITableViewStyleGrouped

You have to set it on the initialisation of UITableView.

  • Set from StoryBoard

OR

UITableView* table = [[UITableView alloc]initWithFrame:myFrame style:UITableViewStyleGrouped]; 

只需将表格样式从“普通”更改为“分组”即可。

subclass the UITableView and override this method

- (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

If you will upload it to the AppStore; Then you have two other options

  1. Adding a normal cell instead of the section header
  2. If you have only one section, then you can simply use table header instead of section header

我遇到了同样的问题,当我浏览时,我来到了这个链接, 请参阅接受的答案,我得出的结论是,您需要将 tableViewHeader Style 设置为 Group 并且它可以正常工作。

if you ve more than one section, you can customize it using some thirdt party API

else you can try this using Grouped table instead of plain 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

 -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView *sectionView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];

        tableView.tableHeaderView =sectionView;

        return sectionView;
    }

您可以更改 tableView 的样式。

let tableView = UITableView(frame: .zero, style: .grouped)

更改UITableViewStyle表从UITableViewStylePlainUITableViewStyleGrouped ,这将使头与细胞向上滚动。

Swift:

As Hani Ibrahim pointed out you need to subclass UITableView. Apple says you : 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:

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)

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.

Disable scrolling section header by changing the property from Storyboard

Storyboard

  1. Attributes inspector
  2. Style
  3. Change Style from plain to Grouped from the dropdown

图片

I'm also facing the issue on scrolling cell my cellForRowAtIndexPath is overlapping with header text.

I've changed the UITableViewProperty from story "Plain" to "Group" and run the application again my issue got resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM