1、sectionHeader粘滞效果是指在tableView向上滑动的时候,当section的header碰到了tableView的顶部的时候,本来应该会继续向上滑从而从视野中退出,但是却留在了tableView的顶部,知道下一个Section的到达然后替代它。
这种效果的实现很简单,只需要tableView的delegate对象实现了UITableViewDelegate中的-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section方法,也就是说使用了自定义的sectionHeader就可以了。
2、sectionHeader的点击展开效果是指点击sectionHeader时候展开、关闭这个section,类似于Xcode左边文件夹的管理。实现这个效果的关键是,这个“展开关闭”的效果是个假象,其实是把这个section的row的数量变为0,也就是通过控制- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section的返回值来实现。
@interface ViewController (){
NSMutableArray * _deleteSecArr; //定义一个成员变量来保存每个section的关闭展开状态
NSInteger _sectionNum; //这是我们预设的section数
}
然后初始化_deleteSecArr:
_sectionNum = 7;
_deleteSecArr = [[NSMutableArray alloc]initWithCapacity:_sectionNum];
for (int i = 0; i< _sectionNum; i++) {
[_deleteSecArr addObject:[NSNumber numberWithBool:NO]];
}
给自定义的header添加tap手势,在手势的方法里面
-(void)tapToOpenOrClose:(id)sender{
UITapGestureRecognizer * tapGR = (UITapGestureRecognizer *)sender;
NSInteger section = tapGR.view.tag;
BOOL flag = [[_deleteSecArr objectAtIndex:section]boolValue];
[_deleteSecArr replaceObjectAtIndex:section withObject:[NSNumber numberWithBool:!flag]];
[self.tableView reloadData];
}
这里通过手势获取手势添加到的view,也就是自定义的SectionHeader,通过把SectionHeader的tag设成它对应的section数就可以把section传递给点击方法里,然后可以把_deleteSecArr里面响应的bool值取反存入。通过reloadData刷新tableView,
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
BOOL close = [[_deleteSecArr objectAtIndex:section] boolValue];
if (close) {
return 0;
}else{
return rowNum; //展开时需要的row数量
}
}
总的来说就是通过点击sectionHeader,在点击手势的方法里面将响应的section的一个bool值改变,刷新tableView的时候会根据这个bool值来提供section里面row的数量。从而实现点击展开和关闭
3、这两个效果在一起使用比较好,因为当tableView很长而且分了很多的section的时候,点击sectionHeader进行关闭效果很好,而粘滞效果保证在你想点击header关闭的时候它都在你的视野里面