关于UITableView Grouped 头部和尾部的空白留于

由于UITableView的Plain模式时,头部会悬停在界面上,当你需要headerView跟随着UITableView一起移动时,最好还是使用Grouped。虽然有使用UIScroViewDelegate的方法实现Plain模式下不悬停,

- (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);
    }
}

但是,是设置contentInset毕竟还是有缺陷,而且没法消除尾部的悬停。所以根据设计的需求,还是使用了Grouped模式。

使用Grouped模式时,出现的问题,section与section之间默认就有空白,直接调用UITableView的dataSource方法,headerForHeight和footerForHeight,return 0是没有用的,上网找了一下,需要设置 

        myTableView.sectionHeaderHeight = 0.0

        myTableView.sectionFooterHeight = 0.0

先消除section于section之间的空白,再调用 headerForHeight和footerForHeight去返回需要自己定义的headerView和footerView。

最后还发现尾部只用有一段留白,网上各种说

        self.automaticallyAdjustsScrollViewInsets = false

        self.edgesForExtendedLayout = .None

 ,但是基本上没什么卵用,最后用这一句消除了尾部的空白:

        tableView.tableFooterView = UIView(frame: CGRectMake(0,0,self.view.width(),0.001))

或者,在footerForHeight返回return 0.001,返回0是没用的,只有return 0.001

 

以上测试为XCODE 7.1 , IOS 8.0以上

 

posted on 2015-11-26 14:52  pang谦  阅读(4287)  评论(0编辑  收藏  举报

导航