[10秒学会] - iOS tableView中headView和footView悬停方式

效果图头部悬停 底部不悬停
[10秒学会] - iOS tableView中headView和footView悬停方式_第1张图片

一共4种

一: 2个都悬停 UITableViewStylePlain  //没什么好疑问的
二:2个都不悬停 UITableViewStyleGrouped //也没啥好疑问的
三:头部不悬停 底部悬停 使用UITableViewStylePlain //网上已经很多这个代码 还是贴一下吧

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {  
//    if (scrollView == self.myTableView)  
    {  
        //YOUR_HEIGHT 为最高的那个headerView的高度  
//        CGFloat sectionHeaderHeight = 20;  
        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);  
        }  
    }  
} 

四 才是今天我们的重点 头部悬停 底部不悬停
 

    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, -105, 0); //首先改变内边距 -105是底部的距离
    CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:2]]; //现在是写死的 你可以根据模型数据 写成变量
    CGRect rect = [self.tableView convertRect:rectInTableView toView:[self.tableView superview]];//这是是最后cell row的高度
    if(rect.origin.y<=-1288){ //-1288  这个位置 是可以根据你的位置调整出来
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }else{
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, -105, 0);
    }

 

你可能感兴趣的:([10秒学会] - iOS tableView中headView和footView悬停方式)