ios UITableView實現(xiàn)無數(shù)據(jù)加載占位圖片
本文介紹了ios UITableView實現(xiàn)無數(shù)據(jù)占位圖片,分享給大家,具體如下:
國際慣例,上效果圖
該效果的實現(xiàn)主要是使用runtime的交叉方法實現(xiàn),將tableView的reloadData與自定義的kk_reloadData交換。新建tableView的Category。
交換方法主要代碼
+ (void)swizzleInstanceSelector:(SEL)originalSel WithSwizzledSelector:(SEL)swizzledSel { Method originMethod = class_getInstanceMethod(self, originalSel); Method swizzedMehtod = class_getInstanceMethod(self, swizzledSel); BOOL methodAdded = class_addMethod(self, originalSel, method_getImplementation(swizzedMehtod), method_getTypeEncoding(swizzedMehtod)); if (methodAdded) { class_replaceMethod(self, swizzledSel, method_getImplementation(originMethod), method_getTypeEncoding(originMethod)); }else{ method_exchangeImplementations(originMethod, swizzedMehtod); } }
交換reloadData
+ (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [self swizzleInstanceSelector:@selector(reloadData) WithSwizzledSelector:@selector(kk_reloadData)]; }); }
kk_reloadData方法,先檢查是否有數(shù)據(jù),再次kk_reloadData方法此時已使用runtime的交換方法則則實際上調(diào)用的是系統(tǒng)的reloadData方法。
- (void)kk_reloadData { [self kk_checkEmpty]; [self kk_reloadData]; }
kk_checkEmpty方法
- (void)kk_checkEmpty { BOOL isEmpty = YES; id<UITableViewDataSource> src = self.dataSource; NSInteger sections = 1; if ([src respondsToSelector:@selector(numberOfSectionsInTableView:)]) { sections = [src numberOfSectionsInTableView:self]; } for (int i = 0; i < sections; i++) { NSInteger rows = [src tableView:self numberOfRowsInSection:i]; if (rows) { isEmpty = NO; } } if (isEmpty) {//數(shù)據(jù)為空,在這里添加視圖 }else{//數(shù)據(jù)不為空,在這里一處視圖 } }
為了降低代碼的侵入,可以給tableView動態(tài)添加一個View屬性即是占位圖視圖。
@property (nonatomic, strong) UIView *placeHolderView;
- (void)setPlaceHolderView:(UIView *)placeHolderView { objc_setAssociatedObject(self, @selector(placeHolderView), placeHolderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (UIView *)placeHolderView { return objc_getAssociatedObject(self, @selector(placeHolderView)); }
kk_checkEmpty的
if (isEmpty) {//數(shù)據(jù)為空,在這里添加視圖 }else{//數(shù)據(jù)不為空,在這里一處視圖 }
修改為
if (isEmpty) { [self.placeHolderView removeFromSuperview]; [self addSubview:self.placeHolderView]; }else{ [self.placeHolderView removeFromSuperview]; }
以后使用的時候只需設(shè)置tableView的placeHolderView屬性即可
_tableView.placeHolderView = [[UIView alloc] init];
打完收工
github地址: https://github.com/wuzaozhou/UITableView-placeholder
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
OC - 9.基于Quartz2D繪制下載進(jìn)度條(demo)
這篇文章主要介紹了OC - 9.基于Quartz2D繪制下載進(jìn)度條(demo)的相關(guān)資料,需要的朋友可以參考下2015-11-11解決ios手機(jī)中input輸入框光標(biāo)過長的問題
在項目中做移動端頁面,發(fā)現(xiàn)IOS 的光標(biāo)大小很大,和安卓的完全不一樣,怎么來調(diào)整大小呢?下面小編給大家?guī)砹薸os手機(jī)中input輸入框光標(biāo)過長問題的解決方法,一起看看吧2018-08-08IOS NSNotification 鍵盤遮擋問題的解決辦法
這篇文章主要介紹了IOS NSNotification 鍵盤遮擋問題的解決辦法的相關(guān)資料,希望通過本文能幫助到大家,解決這樣的問題,需要的朋友可以參考下2017-09-09關(guān)于iOS 11的一些新特性適配實踐總結(jié)
iOS 11 為整個生態(tài)系統(tǒng)的 UI 元素帶來了一種更加大膽、動態(tài)的新風(fēng)格。下面這篇文章主要給大家總結(jié)介紹了關(guān)于iOS 11的一些新特性適配實踐,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-11-11