iOS造輪子系列-TableView空數(shù)據(jù)顯示占位圖片 runtime實(shí)現(xiàn)

大多數(shù)app刷新tableView的時(shí)候, 如果tabview沒有數(shù)據(jù), 則會顯示占位圖片, 這個(gè)怎么做呢? 我想到很多種方案:

  • 1.使用-(void)loadView方法, 根據(jù)是否有數(shù)據(jù)加載不同的視圖
  • 2.在基類的TabviewController中監(jiān)聽當(dāng)前控制器存放數(shù)據(jù)的數(shù)組,如果數(shù)組有值則刷新, 如果沒有值則在上面蓋一個(gè)視圖顯示圖片
  • 3.hook Tabview的reloadData方法, 根據(jù)dataSource的數(shù)據(jù)來判斷是否顯示圖片
    前兩種方法耦合性較高一些, 這里我使用第三種方法演示一遍, 僅給一些人提供一些思路
效果.gif

寫一個(gè)UITableView的分類

@interface UITableView (placeholder)

/* 占位圖 */
@property (nonatomic, strong) UIView *placeHolderView;
@end
#import "UITableView+placeholder.h"
#import <objc/runtime.h>

@implementation NSObject (swizzle)

+ (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);
    }
}

@end



@implementation UITableView (placeholder)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self swizzleInstanceSelector:@selector(reloadData) WithSwizzledSelector:@selector(gy_reloadData)];
    });
    
}

- (void)setPlaceHolderView:(UIView *)placeHolderView
{
    objc_setAssociatedObject(self, @selector(placeHolderView), placeHolderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)placeHolderView
{
    return objc_getAssociatedObject(self, @selector(placeHolderView));
}


- (void)gy_reloadData
{
    [self gy_checkEmpty];
    [self gy_reloadData];
}
- (void)gy_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) {
        [self.placeHolderView removeFromSuperview];
        [self addSubview:self.placeHolderView];
    }else{
        [self.placeHolderView removeFromSuperview];
    }
}

則使用如下: 下面的GYNoDataView就是自定義的視圖(在沒有數(shù)據(jù)的時(shí)候顯示)

_tableView.placeHolderView = [[GYNoDataView alloc] initWithFrame:self.view.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
            NSLog(@"點(diǎn)擊了沒有更多數(shù)據(jù)的圖片");

        }];
[_tableView reloadData];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容