tableview的header和footer取消懸停或者是粘滯


tableview的header和footer取消懸停或者是粘滯,網上找的有效方法是用

-(void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.tag == 100102) { UITableView *tableview = (UITableView *)scrollView; CGFloat sectionHeaderHeight = 20; CGFloat sectionFooterHeight = 20; CGFloat offsetY = tableview.contentOffset.y; if (offsetY >= 0 && offsetY <= sectionHeaderHeight) { tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0); }else if (offsetY >= sectionHeaderHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight) { tableview.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0); }else if (offsetY >= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height) { tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -(tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight), 0); } } } 

但有個問題,若有下拉刷新,那么整個tableview的樣式會發生改變。因為下拉刷新也是靠contentInset設置。
簡單的方法是設置tableview的style為UITableViewStyleGrouped。這樣接解決了懸浮問題。但又會引發新問題,默認下section之間的間距很大,僅僅單獨設置header或footer的高度是不行的。顯示效果的各個section間距其實是section頭部和底部的組合。所以得組合設置才有效果

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 0.01f; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)]; view.backgroundColor = [UIColor clearColor]; return view; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 10; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)]; view.backgroundColor = [UIColor clearColor]; return view; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM