iOS UITableView 無數(shù)據(jù)占位圖

在使用TableView的時(shí)候,經(jīng)常會(huì)遇到一些無數(shù)據(jù)需要用到占位圖的情況,所以就寫一個(gè)控件來實(shí)現(xiàn)。
思路:使用UITableView Category來實(shí)現(xiàn),由于占位圖式樣多種多樣,所以控件就不用考慮很多情況,把占位圖交給外部實(shí)現(xiàn),控件內(nèi)部只處理有數(shù)據(jù)和沒有數(shù)據(jù)顯示占位圖即可。

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@protocol DTableViewPlaceHolderDelegate <NSObject>

@required

/**
 無數(shù)據(jù)占位圖
 @return 占位圖
 */
- (UIView *)makePlaceHolderView;

@optional

/**
 出現(xiàn)占位圖的時(shí)候TableView是否能拖動(dòng)
 @return BOOL
 */
- (BOOL)enableScrollWhenPlaceHolderViewShowing;

@end

@interface UITableView (PlaceHolder)

- (void)d_reloadData;

@end

NS_ASSUME_NONNULL_END

#import "UITableView+PlaceHolder.h"
#import <objc/runtime.h>

@interface UITableView ()

/**占位圖*/
@property (nonatomic, strong) UIView *placeHolderView;

@end


@implementation UITableView (PlaceHolder)

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

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

- (void)d_reloadData {
    [self reloadData];
    [self d_checkEmpty];
}

- (void)d_checkEmpty {
    BOOL isEmpty = YES;
    
    id<UITableViewDataSource> src = self.dataSource;
    NSInteger sections = 1;
    // 獲取sections
    if ([src respondsToSelector: @selector(numberOfSectionsInTableView:)]) {
        sections = [src numberOfSectionsInTableView:self];
    }
    // 獲取rows
    for (int i = 0; i<sections; ++i) {
        NSInteger rows = [src tableView:self numberOfRowsInSection:i];
        // 如果rows不為零
        if (rows > 0) {
            isEmpty = NO;
            break;
        }
        
    }
    
    // 如果rows為空
    if (isEmpty) {
    
        // 默認(rèn)ScrollView可以滾動(dòng)
        BOOL scrollEnabled = YES;
        if ([self respondsToSelector:@selector(enableScrollWhenPlaceHolderViewShowing)]) {
            scrollEnabled = [self performSelector:@selector(enableScrollWhenPlaceHolderViewShowing)];
        }
        else if ([self.delegate respondsToSelector:@selector(enableScrollWhenPlaceHolderViewShowing)]) {
            scrollEnabled = [self.delegate performSelector:@selector(enableScrollWhenPlaceHolderViewShowing)];
        }
        self.scrollEnabled = scrollEnabled;
        
        // 移除placeHolderView
        [self.placeHolderView removeFromSuperview];
        // 獲取placeHolderView
        if ([self respondsToSelector:@selector(makePlaceHolderView)]) {
            self.placeHolderView = [self performSelector:@selector(makePlaceHolderView)];
        }
        else if ( [self.delegate respondsToSelector:@selector(makePlaceHolderView)]) {
            self.placeHolderView = [self.delegate performSelector:@selector(makePlaceHolderView)];
        }
        self.placeHolderView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        [self addSubview:self.placeHolderView];
    }
    else {
        // rows不為空 移除placeHolderView
        [self.placeHolderView removeFromSuperview];
        self.placeHolderView = nil;
        // 設(shè)置TableView 可滾動(dòng)
        self.scrollEnabled = YES;
    }
}

@end

使用時(shí)候只要調(diào)用d_reloadData方法即可。但是必須實(shí)現(xiàn)

- (UIView *)makePlaceHolderView ;

如果需要在沒有數(shù)據(jù)的時(shí)候讓TableView的拖動(dòng)不可用,需要實(shí)現(xiàn)

- (BOOL)enableScrollWhenPlaceHolderViewShowing{
    return NO;
}

Demo

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

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

  • 一、簡(jiǎn)介 <<UITableView(或簡(jiǎn)單地說,表視圖)的一個(gè)實(shí)例是用于顯示和編輯分層列出的信息的一種手段 <<...
    無邪8閱讀 10,969評(píng)論 3 3
  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,307評(píng)論 3 38
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,689評(píng)論 1 32
  • 文/大大大大峰哥 是時(shí)候和17年說一聲再見了. 首先梳理下一七年所做一些事情. 每一個(gè)時(shí)間都有相應(yīng)的任務(wù)與安排,學(xué)...
    大大大大峰哥閱讀 259評(píng)論 0 0
  • 我們常常面對(duì)尋常之物卻沒有看見它本身 其實(shí)我們看了卻沒有看見 有心的人才能夠發(fā)現(xiàn)秘境 在你發(fā)現(xiàn)之前對(duì)你來說只是尋常...
    浪子谷木青閱讀 290評(píng)論 1 1

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