UITableView removes the header/footer stickiness problem, which I have also encountered and solved. Today, a friend asked me, so I will return to this problem, which is not very clever, but some small experience.

Take away the stickiness and go to Baidu and you’ll see stacks of code that are almost the same:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat sectionHeaderHeight = 40; if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) { scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0); } else if (scrollView.contentOffset.y>=sectionHeaderHeight) { scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0); }}Copy the code

The above code is the more common method on the Internet, now to do an analysis, why to use the above method? If you use UITableViewStylePlain, it’s sticky, if you don’t need stickiness, UITableViewStyleGroup is the best choice, and then with the above method involved, why doesn’t it work, Since the above code only works for one group or groups with multiple headers of the same height, if I have an undefined group with an undefined height, the above method is useless, so why not use the UITableViewStyleGroup mode?

Some people say that by default the header footer, header and footer, if you don’t write header and footer proxies or if you write 0, both header and footer will be displayed and by default the footer will be non-zero, I don’t know how many, If you don’t want to use either header or footer you can always hide it by setting it to a very small height:

- (CGFloat) tableView: (UITableView *) tableView heightForHeaderInSection: (NSInteger) section {return 0.001; // cannot be 0, Otherwise the default height} - (CGFloat) tableView: (UITableView *) tableView heightForFooterInSection: (NSInteger) section {return 0.001; // Can not be 0, otherwise the default height}Copy the code

Header and footer both have default height, so when you set the header height, you will find that there is always a bit too much, about 20, that is actually the height of the footer, the so-called header footer, you just need to follow the above method, the naked eye is not detected, and then you can pass

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Copy the code

From defining the contents of the header. In this case, all you need to do to get rid of stickiness is use group mode. In the case of multiple groups and different headers, the best solution is to trick the naked eye by setting the footer to a minimum, then setting the header height and customizing it.

Summary: If grouping mode is used, header and footer height must be set, remember, do not need to use it to set to 0.001 minimum and cannot be 0; Use plain mode, so it is best to not group (tableView. TableHeaderView as we all know, this method does not need to set grouping), if use the grouping and want to remove the sticky, if the header height is same is the case, The proxy method of the top scrollView can work, otherwise choose the group mode. The specific method has been given above, please take a look carefully, if you have any questions, welcome to add group to ask.