You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

246 lines
7.3 KiB

//
// UIView+TFEmptyView.m
// WXReader
//
// Created by 谢腾飞 on 2020/11/20.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "UIView+TFEmptyView.h"
#import <objc/runtime.h>
#import "TFEmptyView.h"
@implementation UIView (TFEmptyView)
+ (void)exchangeInstanceMethod1:(SEL)method1 method2:(SEL)method2
{
method_exchangeImplementations(class_getInstanceMethod(self, method1), class_getInstanceMethod(self, method2));
}
static char kEmptyViewKey;
- (void)setXtfei_emptyView:(TFEmptyView *)xtfei_emptyView
{
if (xtfei_emptyView != self.xtfei_emptyView) {
objc_setAssociatedObject(self, &kEmptyViewKey, xtfei_emptyView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
for (UIView *view in self.subviews) {
if ([view isKindOfClass:[TFEmptyView class]]) {
[view removeFromSuperview];
}
}
[self addSubview:self.xtfei_emptyView];
}
}
- (TFEmptyView *)xtfei_emptyView
{
return objc_getAssociatedObject(self, &kEmptyViewKey);
}
- (NSInteger)totalDataCount
{
NSInteger totalCount = 0;
if ([self isKindOfClass:[UITableView class]]) {
UITableView *tableView = (UITableView *)self;
for (NSInteger section = 0; section < tableView.numberOfSections; section++) {
totalCount += [tableView numberOfRowsInSection:section];
}
} else if ([self isKindOfClass:[UICollectionView class]]) {
UICollectionView *collectionView = (UICollectionView *)self;
for (NSInteger section = 0; section < collectionView.numberOfSections; section++) {
totalCount += [collectionView numberOfItemsInSection:section];
}
}
return totalCount;
}
- (void)getDataAndSet
{
if (!self.xtfei_emptyView) return;
if ([self totalDataCount] == 0) {
[self show];
} else {
[self hide];
}
}
- (void)show
{
if (!self.xtfei_emptyView.autoShowEmptyView) {
self.xtfei_emptyView.hidden = YES;
return;
}
[self xtfei_showEmptyView];
}
- (void)hide
{
if (!self.xtfei_emptyView.autoShowEmptyView) {
self.xtfei_emptyView.hidden = YES;
return;
}
[self xtfei_hideEmptyView];
}
- (void)xtfei_showEmptyView
{
[self.xtfei_emptyView.superview layoutSubviews];
self.xtfei_emptyView.hidden = NO;
[self bringSubviewToFront:self.xtfei_emptyView];
UITableView *tableView = (UITableView *)self;
if ([tableView isKindOfClass:UITableView.class]) {
tableView.mj_footer.hidden = YES;
}
}
- (void)xtfei_hideEmptyView
{
self.xtfei_emptyView.hidden = YES;
UITableView *tableView = (UITableView *)self;
if ([tableView isKindOfClass:UITableView.class]) {
tableView.mj_footer.hidden = NO;
}
}
- (void)xtfei_startLoading
{
self.xtfei_emptyView.hidden = YES;
}
- (void)xtfei_endLoading
{
UITableView *tableView = (UITableView *)self;
if ([tableView isKindOfClass:UITableView.class]) {
tableView.mj_footer.hidden = ![self totalDataCount];
}
self.xtfei_emptyView.hidden = [self totalDataCount];
}
@end
#pragma Mark UITableView
@implementation UITableView (TFEmptyView)
+ (void)load
{
[self exchangeInstanceMethod1:@selector(reloadData) method2:@selector(xtfei_reloadData)];
[self exchangeInstanceMethod1:@selector(insertSections:withRowAnimation:) method2:@selector(xtfei_insertSections:withRowAnimation:)];
[self exchangeInstanceMethod1:@selector(deleteSections:withRowAnimation:) method2:@selector(xtfei_deleteSections:withRowAnimation:)];
[self exchangeInstanceMethod1:@selector(reloadSections:withRowAnimation:) method2:@selector(xtfei_reloadSections:withRowAnimation:)];
[self exchangeInstanceMethod1:@selector(insertRowsAtIndexPaths:withRowAnimation:) method2:@selector(xtfei_insertRowsAtIndexPaths:withRowAnimation:)];
[self exchangeInstanceMethod1:@selector(deleteRowsAtIndexPaths:withRowAnimation:) method2:@selector(xtfei_deleteRowsAtIndexPaths:withRowAnimation:)];
[self exchangeInstanceMethod1:@selector(reloadRowsAtIndexPaths:withRowAnimation:) method2:@selector(xtfei_reloadRowsAtIndexPaths:withRowAnimation:)];
}
- (void)xtfei_reloadData
{
[self xtfei_reloadData];
[self getDataAndSet];
}
- (void)xtfei_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
{
[self xtfei_insertSections:sections withRowAnimation:animation];
[self getDataAndSet];
}
- (void)xtfei_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
{
[self xtfei_deleteSections:sections withRowAnimation:animation];
[self getDataAndSet];
}
- (void)xtfei_reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
{
[self xtfei_reloadSections:sections withRowAnimation:animation];
[self getDataAndSet];
}
- (void)xtfei_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
{
[self xtfei_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
[self getDataAndSet];
}
- (void)xtfei_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
{
[self xtfei_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
[self getDataAndSet];
}
- (void)xtfei_reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
{
[self xtfei_reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
[self getDataAndSet];
}
@end
#pragma Mark UICollectionView
@implementation UICollectionView (TFEmptyView)
+ (void)load
{
[self exchangeInstanceMethod1:@selector(reloadData) method2:@selector(xtfei_reloadData)];
[self exchangeInstanceMethod1:@selector(insertSections:) method2:@selector(xtfei_insertSections:)];
[self exchangeInstanceMethod1:@selector(deleteSections:) method2:@selector(xtfei_deleteSections:)];
[self exchangeInstanceMethod1:@selector(reloadSections:) method2:@selector(xtfei_reloadSections:)];
[self exchangeInstanceMethod1:@selector(insertItemsAtIndexPaths:) method2:@selector(xtfei_insertItemsAtIndexPaths:)];
[self exchangeInstanceMethod1:@selector(deleteItemsAtIndexPaths:) method2:@selector(xtfei_deleteItemsAtIndexPaths:)];
[self exchangeInstanceMethod1:@selector(reloadItemsAtIndexPaths:) method2:@selector(xtfei_reloadItemsAtIndexPaths:)];
}
- (void)xtfei_reloadData
{
[self xtfei_reloadData];
[self getDataAndSet];
}
- (void)xtfei_insertSections:(NSIndexSet *)sections
{
[self xtfei_insertSections:sections];
[self getDataAndSet];
}
- (void)xtfei_deleteSections:(NSIndexSet *)sections
{
[self xtfei_deleteSections:sections];
[self getDataAndSet];
}
- (void)xtfei_reloadSections:(NSIndexSet *)sections
{
[self xtfei_reloadSections:sections];
[self getDataAndSet];
}
- (void)xtfei_insertItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
[self xtfei_insertItemsAtIndexPaths:indexPaths];
[self getDataAndSet];
}
- (void)xtfei_deleteItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
[self xtfei_deleteItemsAtIndexPaths:indexPaths];
[self getDataAndSet];
}
- (void)xtfei_reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
[self xtfei_reloadItemsAtIndexPaths:indexPaths];
[self getDataAndSet];
}
@end