iOS自定义控件:自定义TableView、CollectionView空数据占位图

最近由于业务需求,需要封装这样的一个提示页面。看了网上方法感觉都大同小异,其中DZNEmptyDataSet是很好的一个库,但是对我个人而言有点大财小用了。所以就借鉴一下其方法,自己封装一个。感觉有更高的自定义性吧。(我是初学者,请大佬爱护,勿喷) 一、封装代码:利用UIScrollView的分类实现

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIScrollView (WD_NoData)
// 需要显示的占位页面
@property (nonatomic, strong) UIView *noDataView;

@end

NS_ASSUME_NONNULL_END

复制代码

实现代码

#import "UIScrollView+WD_NoData.h"
#import <objc/runtime.h>

static char *noDataViewKey = "noDataViewKey";

@implementation UIScrollView (WD_NoData)

/**
 交换方法

 @param sel1 原方法
 @param sel2 自定义方法
 @param cls 类
 */
void exchangeSelector(SEL sel1, SEL sel2, Class cls) {
    Class class = [cls class];
    Method originalMethod = class_getInstanceMethod(class, sel1);
    Method swizzledMethod = class_getInstanceMethod(class, sel2);
    BOOL success = class_addMethod(class, sel1, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    if (success) {
        class_replaceMethod(class, sel2, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

#pragma mark =============== Setter ===============
- (void)setNoDataView:(UIView *)noDataView {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        exchangeSelector(@selector(reloadData), @selector(wd_reloadData), [self class]);
    });
    
    objc_setAssociatedObject(self, noDataViewKey, noDataView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark =============== Getter ===============
- (UIView *)noDataView {
    UIView *noDataView = objc_getAssociatedObject(self, noDataViewKey);
    noDataView.frame = self.frame;
    return noDataView;
}


- (void)wd_reloadData {
    [self wd_reloadData];
    [self wd_checkData];
}

#pragma mark =============== 获取数据 ===============
- (void)wd_checkData {
    NSInteger items = 0;
    
    if (![self respondsToSelector:@selector(dataSource)]) {
        return;
    }
    
    // UITableView support
    if ([self isKindOfClass:[UITableView class]]) {
        
        UITableView *tableView = (UITableView *)self;
        id <UITableViewDataSource> dataSource = tableView.dataSource;
        
        NSInteger sections = 1;
        
        if (dataSource && [dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
            sections = [dataSource numberOfSectionsInTableView:tableView];
        }
        
        if (dataSource && [dataSource respondsToSelector:@selector(tableView:numberOfRowsInSection:)]) {
            for (NSInteger section = 0; section < sections; section++) {
                items += [dataSource tableView:tableView numberOfRowsInSection:section];
            }
        }
    }
    // UICollectionView support
    else if ([self isKindOfClass:[UICollectionView class]]) {
        
        UICollectionView *collectionView = (UICollectionView *)self;
        id <UICollectionViewDataSource> dataSource = collectionView.dataSource;
        
        NSInteger sections = 1;
        
        if (dataSource && [dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]) {
            sections = [dataSource numberOfSectionsInCollectionView:collectionView];
        }
        
        if (dataSource && [dataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)]) {
            for (NSInteger section = 0; section < sections; section++) {
                items += [dataSource collectionView:collectionView numberOfItemsInSection:section];
            }
        }
    }
    
    if ( items == 0 ) {
        [self.superview addSubview:self.noDataView];
    } else {
        [self.noDataView removeFromSuperview];
    }
}
@end
复制代码

二、使用方法

#import "UIScrollView+WD_NoData.h"
// 自定义页面
#import "WDTestEmptyView.h"
复制代码
//示例的视图
 WDTestEmptyView *view = [NSBundle.mainBundle loadNibNamed:@"WDTestEmptyView" owner:self options:nil].firstObject;
// 设置视图
self.tableView.noDataView = view;
复制代码

三、效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值