美文网首页iOS常用
iOS 给Tableview 没有数据的时候添加占位图

iOS 给Tableview 没有数据的时候添加占位图

作者: 赵哥窟 | 来源:发表于2021-07-19 10:51 被阅读0次

一般iOS在UITableView没有数据的时候为了界面好看一点,UI会给这些没有数据的页面设计一些占位图,有的时候是纯图片,有的时候会有一些按钮操作。

实现也很简单就是为UITableView 添加一个Category

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@protocol DTableViewPlaceHolderDelegate <NSObject>

@required

/**
 无数据占位图
 @return 占位图
 */
- (UIView *)makePlaceHolderView;

@optional

/**
 出现占位图的时候TableView是否能拖动
 @return BOOL
 */
- (BOOL)enableScrollWhenPlaceHolderViewShowing;

@end

@interface UITableView (PlaceHolder)

@property (nonatomic, weak, nullable) id <DTableViewPlaceHolderDelegate> placeHolderDelegate;

- (void)d_reloadData;

@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);
}

-(id)placeHolderDelegate{
    return objc_getAssociatedObject(self, @selector(placeHolderDelegate));
}

- (void)setPlaceHolderDelegate:(id<DTableViewPlaceHolderDelegate>)placeHolderDelegate{
    objc_setAssociatedObject(self, @selector(placeHolderDelegate), placeHolderDelegate, 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) {
    
        // 模式ScrollView可以滚动
        BOOL scrollEnabled = YES;
        if ([self respondsToSelector:@selector(enableScrollWhenPlaceHolderViewShowing)]) {
            scrollEnabled = [[self performSelector:@selector(enableScrollWhenPlaceHolderViewShowing)] boolValue];
        }
        else if ([self.delegate respondsToSelector:@selector(enableScrollWhenPlaceHolderViewShowing)]) {
            scrollEnabled = [[self.delegate performSelector:@selector(enableScrollWhenPlaceHolderViewShowing)] boolValue];
        }
        self.scrollEnabled = scrollEnabled;
        
        // 移除placeHolderView
        [self.placeHolderView removeFromSuperview];
        // 获取placeHolderView
        if ([self respondsToSelector:@selector(makePlaceHolderView)]) {
            self.placeHolderView = [self performSelector:@selector(makePlaceHolderView)];
        }
        
        if ( [self.placeHolderDelegate respondsToSelector:@selector(makePlaceHolderView)]) {
            self.placeHolderView = [self.placeHolderDelegate 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;
        // 设置TableView 可滚动
        self.scrollEnabled = YES;
    }
}

@end
使用,现实DTableViewPlaceHolderDelegate
  self.tableView.placeHolderDelegate = self;
// 刷新数据调用
[self.tableView d_reloadData];
- (UIView *)makePlaceHolderView {
    return  [TaskPlaceholder loadXibView];;
}

Xib/纯代码的View都可以。如果有按钮事件就在自定义的View里面加block即可

- (UIView *)makePlaceHolderView {
    TaskPlaceholder *view = [TaskPlaceholder loadXibView];
    view.emptyOverlayClicked = ^{
      // 做一些按钮操作
    };
    return view;
}

相关文章

网友评论

    本文标题:iOS 给Tableview 没有数据的时候添加占位图

    本文链接:https://www.haomeiwen.com/subject/elmcmltx.html