123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- //
- // UIScrollView+SREmptyData.m
- // SREmptyDataViewDemo
- //
- // Created by https://github.com/guowilling on 2018/1/11.
- // Copyright © 2018年 SR. All rights reserved.
- //
- #import "UIScrollView+SREmptyData.h"
- #import "SREmptyDataView.h"
- #import <objc/runtime.h>
- @implementation UIScrollView (SREmptyData)
- //static const char * kEmptyDataViewKey = "emptyDataViewKey";
- //static const void * kEmptyDataViewKey = "emptyDataViewKey";
- static const void * kEmptyDataViewKey = &kEmptyDataViewKey;
- - (void)setSr_emptyDataView:(SREmptyDataView *)sr_emptyDataView {
- objc_setAssociatedObject(self, &kEmptyDataViewKey, sr_emptyDataView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
-
- // remove old sr_emptyDataView if exist
- for (UIView *view in self.subviews) {
- if ([view isKindOfClass:[SREmptyDataView class]]) {
- [view removeFromSuperview];
- }
- }
- [self addSubview:sr_emptyDataView];
- }
- - (SREmptyDataView *)sr_emptyDataView {
- return objc_getAssociatedObject(self, &kEmptyDataViewKey);
- }
- #pragma mark - Private Method
- - (void)showOrHideEmptyView {
- if (self.sr_emptyDataView.autoManagement) {
- if ([self shouldShowEmptyView]) {
- [self sr_showEmptyDataView];
- } else {
- [self sr_hideEmptyDataView];
- }
- }
- }
- - (BOOL)shouldShowEmptyView {
- BOOL flag = YES;
- NSInteger dataCount = 0;
- if ([self isKindOfClass:[UITableView class]]) {
- UITableView *tableView = (UITableView *)self;
- for (NSInteger section = 0; section < tableView.numberOfSections; section++) {
- dataCount += [tableView numberOfRowsInSection:section];
- }
- }
- if ([self isKindOfClass:[UICollectionView class]]) {
- UICollectionView *collectionView = (UICollectionView *)self;
- for (NSInteger section = 0; section < collectionView.numberOfSections; section++) {
- dataCount += [collectionView numberOfItemsInSection:section];
- }
- }
- if (dataCount > 0) {
- flag = NO;
- }
- return flag;
- }
- #pragma mark - Public Method
- - (void)sr_showEmptyDataView {
- self.sr_emptyDataView.hidden = NO;
- [self.sr_emptyDataView.superview layoutSubviews];
- [self bringSubviewToFront:self.sr_emptyDataView];
- }
- - (void)sr_hideEmptyDataView {
- self.sr_emptyDataView.hidden = YES;
- }
- - (void)sr_startLoadingData {
- [self sr_hideEmptyDataView];
- }
- - (void)sr_endLoadingData {
- if ([self shouldShowEmptyView]) {
- [self sr_showEmptyDataView];
- } else {
- [self sr_hideEmptyDataView];
- }
- }
- @end
- @implementation UITableView (Empty)
- + (void)load {
-
- }
- - (void)sr_reloadData {
- [self sr_reloadData];
- [self showOrHideEmptyView];
- }
- - (void)sr_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
- [self sr_insertSections:sections withRowAnimation:animation];
- [self showOrHideEmptyView];
- }
- - (void)sr_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
- [self sr_insertSections:sections withRowAnimation:animation];
- [self showOrHideEmptyView];
- }
- - (void)sr_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
- [self sr_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
- [self showOrHideEmptyView];
- }
- - (void)sr_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
- [self sr_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
- [self showOrHideEmptyView];
- }
- @end
- @implementation UICollectionView (Empty)
- + (void)load {
-
- }
- - (void)sr_reloadData {
- [self sr_reloadData];
- [self showOrHideEmptyView];
- }
- - (void)sr_insertSections:(NSIndexSet *)sections {
- [self sr_insertSections:sections];
- [self showOrHideEmptyView];
- }
- - (void)sr_deleteSections:(NSIndexSet *)sections {
- [self sr_deleteSections:sections];
- [self showOrHideEmptyView];
- }
- - (void)sr_insertItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
- [self sr_insertItemsAtIndexPaths:indexPaths];
- [self showOrHideEmptyView];
- }
- - (void)sr_deleteItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
- [self sr_deleteItemsAtIndexPaths:indexPaths];
- [self showOrHideEmptyView];
- }
- @end
|