iOS UITableView sectionHeader和sectionFooter 取消悬停效果

需求

当 UITableView 设置为 UITableViewStylePlain,且设置了sectionHeader 和 sectionFooter ,在滑动的时候,头部和尾部会有悬停的效果。

在某些UI搭建中,我们需要取消这个悬停效果。

解决

当然最简单的解决方法就是把 UITableViewStylePlain 修改为 UITableViewStyleGrouped,但是我们的目的是在 UITableViewStylePlain 这个设置下取消这个悬停效果,所以需要用别的方法来达到这个目的。

解决这个问题的思路是,重写 header、footer 的 setFrame: 方法,在 dequeueReusableHeaderFooterViewWithIdentifier 出 header 或者 footer 的时候,设置 frame 为 改变之后的 frame,从而达到取消悬停的效果。

header 里面的关键代码如下:

header.h

@interface SectionHeaderView : UITableViewHeaderFooterView

@property (nonatomic,weak) UITableView *tableView;

@property(nonatomic,assign) NSUInteger section;

@end

header.m

/*
 非悬停header 设置Header的frame为改变之后的frame, 当Header到达顶部的时候,Header就会跟着单元内容一起滚动,不再悬停
 */
- (void)setFrame:(CGRect)frame {
    
    [super setFrame:[_tableView rectForHeaderInSection:_section]];
}

footer 里面的关键代码如下:

footer.h

@interface SectionFooterView : UITableViewHeaderFooterView

@property (nonatomic,weak) UITableView *tableView;

@property(nonatomic,assign) NSUInteger section;

@end

footer.m

/*
 非悬停Footer 设置Footer的frame为改变之后的frame, 当Footer到达底部的时候,Footer就会跟着单元内容一起滚动,不再悬停
 */
- (void)setFrame:(CGRect)frame {
    
    [super setFrame:[_tableView rectForFooterInSection:_section]];
}
Demo

详细请看 Demo

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。