国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

ios UITableView實現無數據加載占位圖片

2019-10-21 18:45:14
字體:
來源:轉載
供稿:網友

本文介紹了ios/208565.html">ios UITableView實現無數據占位圖片,分享給大家,具體如下:

國際慣例,上效果圖

ios,無數據占位圖片,UITableView,占位圖片,數據加載占位

該效果的實現主要是使用runtime的交叉方法實現,將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方法,先檢查是否有數據,再次kk_reloadData方法此時已使用runtime的交換方法則則實際上調用的是系統的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) {//數據為空,在這里添加視圖  }else{//數據不為空,在這里一處視圖  }}

為了降低代碼的侵入,可以給tableView動態添加一個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) {//數據為空,在這里添加視圖}else{//數據不為空,在這里一處視圖}

修改為

if (isEmpty) {    [self.placeHolderView removeFromSuperview];    [self addSubview:self.placeHolderView];  }else{    [self.placeHolderView removeFromSuperview];  }

以后使用的時候只需設置tableView的placeHolderView屬性即可

_tableView.placeHolderView = [[UIView alloc] init];

打完收工

github地址: https://github.com/wuzaozhou/UITableView-placeholder

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蒙山县| 德安县| 淄博市| 平乐县| 奉新县| 江北区| 固始县| 夹江县| 焉耆| 昂仁县| 翁源县| 台江县| 镇坪县| 曲水县| 加查县| 苏尼特右旗| 旬邑县| 安陆市| 措美县| 景谷| 西峡县| 腾冲县| 塘沽区| 东乌珠穆沁旗| 江油市| 彭山县| 社旗县| 四平市| 汪清县| 阳曲县| 沙田区| 阳春市| 肇庆市| 大理市| 姜堰市| 科尔| 乌拉特后旗| 宝山区| 阜阳市| 梨树县| 剑川县|