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

首頁 > 系統(tǒng) > iOS > 正文

ios UITableView實現(xiàn)無數(shù)據(jù)加載占位圖片

2020-07-26 02:44:25
字體:
供稿:網(wǎng)友

本文介紹了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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 沅陵县| 青岛市| 文化| 和林格尔县| 宜州市| 阿拉善左旗| 保山市| 沛县| 漯河市| 威信县| 平南县| 西昌市| 河津市| 东阿县| 肥西县| 丹阳市| 隆化县| 巴楚县| 香格里拉县| 贵南县| 佛坪县| 津市市| 吉水县| 海晏县| 栾川县| 久治县| 平定县| 营山县| 顺昌县| 长岭县| 米泉市| 博兴县| 仪征市| 永德县| 普定县| 阳西县| 烟台市| 博客| 光泽县| 乡城县| 临朐县|