// // 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 @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 *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation { [self sr_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation]; [self showOrHideEmptyView]; } - (void)sr_deleteRowsAtIndexPaths:(NSArray *)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 *)indexPaths { [self sr_insertItemsAtIndexPaths:indexPaths]; [self showOrHideEmptyView]; } - (void)sr_deleteItemsAtIndexPaths:(NSArray *)indexPaths { [self sr_deleteItemsAtIndexPaths:indexPaths]; [self showOrHideEmptyView]; } @end