直接 上代码
满足了tableheaderview的自定义显示和隐藏。这里只是提供一种思路给大家,有更好的方法记得和我分享[坏笑]。view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
view.backgroundColor=[UIColor redColor];
_tableView.tableHeaderView=nil;
[_tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:Nil];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
NSLog(@"change=%@",change);
static int b=0;
if (_tableView.contentOffset.y<=-20 && b%2==0) {
_tableView.tableHeaderView=view;
}else if (_tableView.contentOffset.y>=20 && b%2==1){
_tableView.tableHeaderView=nil;
}
b++;
}
这篇博客介绍了如何在iOS应用中动态地隐藏和显示UITableView的表头视图。通过创建一个红色背景的UIView,并监听UITableView的内容偏移量,当内容偏移量达到特定阈值时,实现表头视图的切换显示。利用KVO(Key-Value Observing)来监听内容偏移量的变化,根据偏移量判断并设置_tableView.tableHeaderView的可见性,从而实现平滑的动画效果。
186

被折叠的 条评论
为什么被折叠?



