让UITableView的headerView或footerView跟随cell一起滚动
2015-08-18 16:07
369 查看
以headerView为例(footerView处理方式类似),以下四种方式均有独到之处:
1、无分区 最简单也最常见。将headerView设置为整个tableView的headerView,而不是 section 0 的headerView
self.tableView.tableHeaderView = headerView。
2、多个section 设置 tableView 的 style 为 UITableViewStyleGrouped,然后
这样确实可以让 headerView ,在滚动tableView的时候,跟随着cell的内容一起滚动。但是cell都被加上了边框,而且cell的水平显示范围变窄了。
但是我们可以尝试调整cell的Frame,或者自定义cell。
3、多个section 将每个分区第一个cell作为分区的headerView
重写cell,就不贴代码了,写的代码较方法2差不多,但是效果非常好。
4、去掉UItableview headerview黏性 (不推荐,但思路不错,方法应该还可以再优化下)
但是,当可滚动的位置太小,scrollView会停在滚到的位置上。
1、无分区 最简单也最常见。将headerView设置为整个tableView的headerView,而不是 section 0 的headerView
self.tableView.tableHeaderView = headerView。
2、多个section 设置 tableView 的 style 为 UITableViewStyleGrouped,然后
<span style="font-size:14px;">- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 分区数; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section == 0) { <span> </span>return 1; // 注意当某个分区不需要headerView时,也需要重新设置高度。(Group时有默认高度) } return 170; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ if (section == 0) { return nil; } return headerView; }</span>
这样确实可以让 headerView ,在滚动tableView的时候,跟随着cell的内容一起滚动。但是cell都被加上了边框,而且cell的水平显示范围变窄了。
但是我们可以尝试调整cell的Frame,或者自定义cell。
3、多个section 将每个分区第一个cell作为分区的headerView
重写cell,就不贴代码了,写的代码较方法2差不多,但是效果非常好。
4、去掉UItableview headerview黏性 (不推荐,但思路不错,方法应该还可以再优化下)
<span style="font-size:14px;">- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView == self.tableView) { CGFloat sectionHeaderHeight = HeaderHeight; 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); } } }</span>
但是,当可滚动的位置太小,scrollView会停在滚到的位置上。
相关文章推荐
- java中StringBuilder、StringBuffer、String类之间的关系
- 排序检索B - List of Conquests
- iOS开发系列--UITableView全面解析
- hdoj 1509 Windows Message Queue
- iOS开发 -- UIViewContentMode各类型效果
- UIAlertView的基本用法
- 黑马程序员_String类型以及StringBuilder
- POJ 1986 DIstance Query LCA水题
- RequireJS Optimizer 的使用和配置
- Reveal UI 分析工具简单使用
- 【Mockplus教程】安装Mockplus
- 【Mockplus教程】界面闪烁花屏怎么办?
- 【Mockplus教程】MAC 安全提示无法安装怎么办?
- Unable to determine the principal end of an association between the types '***. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
- 【Mockplus教程】为什么 Windows XP 无法安装?
- 【Mockplus视频教程】《10分钟玩转Mockplus》
- IOS UITableView拖动排序功能
- [leetcode] 187.Repeated DNA Sequences
- POJ---2299-Ultra-QuickSort
- ios-用xib和UI table View controller 的团购网站