iOS UITableView sectionHeader和sectionFooter 取消懸停效果

需求

當(dāng) UITableView 設(shè)置為 UITableViewStylePlain,且設(shè)置了sectionHeader 和 sectionFooter ,在滑動(dòng)的時(shí)候,頭部和尾部會(huì)有懸停的效果。

在某些UI搭建中,我們需要取消這個(gè)懸停效果。

解決

當(dāng)然最簡(jiǎn)單的解決方法就是把 UITableViewStylePlain 修改為 UITableViewStyleGrouped,但是我們的目的是在 UITableViewStylePlain 這個(gè)設(shè)置下取消這個(gè)懸停效果,所以需要用別的方法來達(dá)到這個(gè)目的。

解決這個(gè)問題的思路是,重寫 header、footer 的 setFrame: 方法,在 dequeueReusableHeaderFooterViewWithIdentifier 出 header 或者 footer 的時(shí)候,設(shè)置 frame 為 改變之后的 frame,從而達(dá)到取消懸停的效果。

header 里面的關(guān)鍵代碼如下:

header.h

@interface SectionHeaderView : UITableViewHeaderFooterView

@property (nonatomic,weak) UITableView *tableView;

@property(nonatomic,assign) NSUInteger section;

@end

header.m

/*
 非懸停header 設(shè)置Header的frame為改變之后的frame, 當(dāng)Header到達(dá)頂部的時(shí)候,Header就會(huì)跟著單元內(nèi)容一起滾動(dòng),不再懸停
 */
- (void)setFrame:(CGRect)frame {
    
    [super setFrame:[_tableView rectForHeaderInSection:_section]];
}

footer 里面的關(guān)鍵代碼如下:

footer.h

@interface SectionFooterView : UITableViewHeaderFooterView

@property (nonatomic,weak) UITableView *tableView;

@property(nonatomic,assign) NSUInteger section;

@end

footer.m

/*
 非懸停Footer 設(shè)置Footer的frame為改變之后的frame, 當(dāng)Footer到達(dá)底部的時(shí)候,F(xiàn)ooter就會(huì)跟著單元內(nèi)容一起滾動(dòng),不再懸停
 */
- (void)setFrame:(CGRect)frame {
    
    [super setFrame:[_tableView rectForFooterInSection:_section]];
}
Demo

詳細(xì)請(qǐng)看 Demo

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容