一、UITableViewStylePlain
1.有多段時(shí) 段頭停留(自帶效果)
2.沒有中間的間距和頭部間距(要想有的重寫UITableViewCell \UITableViewHeaderFooterView里面的setFrame方法)
擴(kuò)展:讓段頭不停留(取消粘性效果)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat sectionHeaderHeight = 30;
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);
}
}
// 擴(kuò)展: 頭尾都不懸停
-(void)scrollViewDidScroll:(UIScrollView)scrollView
{
//#warning 改下頭/尾視圖的高度
CGFloat sectionHeaderHeight =20 ;//(頭視圖的高度)
CGFloat sectionFooterHeight = 44;// (尾視圖的高度)
CGFloat f =scrollView.contentSize.height-scrollView.frame.size.height-scrollView.contentOffset.y-sectionFooterHeight;
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, -1sectionFooterHeight, 0);
}else if ((scrollView.contentOffset.y>=sectionHeaderHeight)&&(f>=0)) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -1sectionFooterHeight, 0);
}else if ((f>=-1sectionFooterHeight)&&(f<=0)){
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -(sectionFooterHeight+f), 0);
}else if (f<-1*sectionFooterHeight){
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}
不過在創(chuàng)建tableView時(shí)還得加行代碼
//44為尾視圖高度
tableView.contentInset = UIEdgeInsetsMake(0, 0, -44, 0);
二、UITableViewStyleGroup
注意:去掉頭部和中間間隔
正確的理解方法
1.設(shè)置標(biāo)頭的高度為特小值 (不能為零 為零的話蘋果會(huì)取默認(rèn)值就無法消除頭部間距了)
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)];
view.backgroundColor = [UIColor redColor];
self.tableView.tableHeaderView = view;
2.寫代理方法(中間的留白其實(shí)是段尾的高度 代理的作用設(shè)置段尾的高度 返回值也不能為0)
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01f;
}
特殊的處理方法也能實(shí)現(xiàn)該效果
1.? ? self.tableView.contentInset = UIEdgeInsetsMake(-44, 0, 0, 0);
2.重寫UITableViewHeaderFooterView的
-(void)setFrame:(CGRect)frame{
frame.size.height+=10;
[super setFrame:frame];
}