溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-07-09 09:38:15 來源:億速云 閱讀:131 作者:小新 欄目:移動開發(fā)

這篇文章主要介紹了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];

打完收工

感謝你能夠認真閱讀完這篇文章,希望小編分享的“ios UITableView如何實現(xiàn)無數(shù)據(jù)加載占位圖片”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI