国产探花免费观看_亚洲丰满少妇自慰呻吟_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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洱源县| 肥西县| 涡阳县| 仙游县| 新源县| 乐山市| 巍山| 天门市| 城市| 新民市| 高雄县| 开平市| 冷水江市| 大安市| 海晏县| 尚义县| 曲周县| 遂川县| 昂仁县| 子洲县| 武城县| 浏阳市| 登封市| 海门市| 南和县| 濮阳市| 类乌齐县| 通州市| 读书| 黑山县| 长春市| 大安市| 乐清市| 正阳县| 新昌县| 达州市| 聂拉木县| 泰州市| 随州市| 山丹县| 扎兰屯市|