UIView+YMEmpty.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // UIView+YMEmpty.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2023/11/6.
  6. //
  7. #import "YMEmptyView.h"
  8. #import "UIView+YMEmpty.h"
  9. #import <objc/runtime.h>
  10. #pragma mark - ------------------ UIView ------------------
  11. @implementation UIView (YMEmpty)
  12. + (void)exchangeInstanceMethod1:(SEL)method1 method2:(SEL)method2
  13. {
  14. method_exchangeImplementations(class_getInstanceMethod(self, method1), class_getInstanceMethod(self, method2));
  15. }
  16. #pragma mark - Setter/Getter
  17. static char kEmptyViewKey;
  18. - (void)setYm_emptyView:(YMEmptyView *)ym_emptyView{
  19. if (ym_emptyView != self.ym_emptyView) {
  20. objc_setAssociatedObject(self, &kEmptyViewKey, ym_emptyView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  21. for (UIView *view in self.subviews) {
  22. if ([view isKindOfClass:[YMEmptyView class]]) {
  23. [view removeFromSuperview];
  24. }
  25. }
  26. [self addSubview:self.ym_emptyView];
  27. if ([self isKindOfClass:[UITableView class]] || [self isKindOfClass:[UICollectionView class]]) {
  28. [self getDataAndSet]; // 添加时根据DataSource去决定显隐
  29. } else {
  30. self.ym_emptyView.hidden = YES;// 添加时默认隐藏
  31. }
  32. }
  33. }
  34. - (YMEmptyView *)ym_emptyView{
  35. return objc_getAssociatedObject(self, &kEmptyViewKey);
  36. }
  37. #pragma mark - Private Method (UITableView、UICollectionView有效)
  38. - (NSInteger)totalDataCount
  39. {
  40. NSInteger totalCount = 0;
  41. if ([self isKindOfClass:[UITableView class]]) {
  42. UITableView *tableView = (UITableView *)self;
  43. for (NSInteger section = 0; section < tableView.numberOfSections; section++) {
  44. totalCount += [tableView numberOfRowsInSection:section];
  45. }
  46. } else if ([self isKindOfClass:[UICollectionView class]]) {
  47. UICollectionView *collectionView = (UICollectionView *)self;
  48. for (NSInteger section = 0; section < collectionView.numberOfSections; section++) {
  49. totalCount += [collectionView numberOfItemsInSection:section];
  50. }
  51. }
  52. return totalCount;
  53. }
  54. - (void)getDataAndSet{
  55. //没有设置emptyView的,直接返回
  56. if (!self.ym_emptyView) {
  57. return;
  58. }
  59. if ([self totalDataCount] == 0) {
  60. [self show];
  61. }else{
  62. [self hide];
  63. }
  64. }
  65. - (void)show{
  66. //当不自动显隐时,内部自动调用show方法时也不要去显示,要显示的话只有手动去调用 ym_showEmptyView
  67. if (!self.ym_emptyView.autoShowEmptyView) {
  68. return;
  69. }
  70. [self ym_showEmptyView];
  71. }
  72. - (void)hide{
  73. //当不自动显隐时,内部自动调用hide方法时也不要去隐藏,要隐藏的话只有手动去调用 ym_hideEmptyView
  74. if (!self.ym_emptyView.autoShowEmptyView) {
  75. return;
  76. }
  77. [self ym_hideEmptyView];
  78. }
  79. #pragma mark - Public Method
  80. - (void)ym_showEmptyView{
  81. NSAssert(![self isKindOfClass:[YMEmptyView class]], @"YMEmptyView及其子类不能调用ym_showEmptyView方法");
  82. self.ym_emptyView.hidden = NO;
  83. //让 emptyBGView 始终保持在最上层
  84. [self bringSubviewToFront:self.ym_emptyView];
  85. }
  86. - (void)ym_hideEmptyView{
  87. NSAssert(![self isKindOfClass:[YMEmptyView class]], @"YMEmptyView及其子类不能调用ym_hideEmptyView方法");
  88. self.ym_emptyView.hidden = YES;
  89. }
  90. - (void)ym_startLoading{
  91. NSAssert(![self isKindOfClass:[YMEmptyView class]], @"YMEmptyView及其子类不能调用ym_startLoading方法");
  92. self.ym_emptyView.hidden = YES;
  93. }
  94. - (void)ym_endLoading{
  95. NSAssert(![self isKindOfClass:[YMEmptyView class]], @"YMEmptyView及其子类不能调用ym_endLoading方法");
  96. self.ym_emptyView.hidden = [self totalDataCount];
  97. }
  98. @end
  99. #pragma mark - ------------------ UITableView ------------------
  100. @implementation UITableView (YMEmpty)
  101. + (void)load{
  102. [self exchangeInstanceMethod1:@selector(reloadData) method2:@selector(ym_reloadData)];
  103. ///section
  104. [self exchangeInstanceMethod1:@selector(insertSections:withRowAnimation:) method2:@selector(ym_insertSections:withRowAnimation:)];
  105. [self exchangeInstanceMethod1:@selector(deleteSections:withRowAnimation:) method2:@selector(ym_deleteSections:withRowAnimation:)];
  106. [self exchangeInstanceMethod1:@selector(reloadSections:withRowAnimation:) method2:@selector(ym_reloadSections:withRowAnimation:)];
  107. ///row
  108. [self exchangeInstanceMethod1:@selector(insertRowsAtIndexPaths:withRowAnimation:) method2:@selector(ym_insertRowsAtIndexPaths:withRowAnimation:)];
  109. [self exchangeInstanceMethod1:@selector(deleteRowsAtIndexPaths:withRowAnimation:) method2:@selector(ym_deleteRowsAtIndexPaths:withRowAnimation:)];
  110. [self exchangeInstanceMethod1:@selector(reloadRowsAtIndexPaths:withRowAnimation:) method2:@selector(ym_reloadRowsAtIndexPaths:withRowAnimation:)];
  111. }
  112. - (void)ym_reloadData{
  113. [self ym_reloadData];
  114. [self getDataAndSet];
  115. }
  116. ///section
  117. - (void)ym_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation{
  118. [self ym_insertSections:sections withRowAnimation:animation];
  119. [self getDataAndSet];
  120. }
  121. - (void)ym_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation{
  122. [self ym_deleteSections:sections withRowAnimation:animation];
  123. [self getDataAndSet];
  124. }
  125. - (void)ym_reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation{
  126. [self ym_reloadSections:sections withRowAnimation:animation];
  127. [self getDataAndSet];
  128. }
  129. ///row
  130. - (void)ym_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation{
  131. [self ym_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
  132. [self getDataAndSet];
  133. }
  134. - (void)ym_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation{
  135. [self ym_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
  136. [self getDataAndSet];
  137. }
  138. - (void)ym_reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation{
  139. [self ym_reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
  140. [self getDataAndSet];
  141. }
  142. @end
  143. #pragma mark - ------------------ UICollectionView ------------------
  144. @implementation UICollectionView (YMEmpty)
  145. + (void)load{
  146. [self exchangeInstanceMethod1:@selector(reloadData) method2:@selector(ym_reloadData)];
  147. ///section
  148. [self exchangeInstanceMethod1:@selector(insertSections:) method2:@selector(ym_insertSections:)];
  149. [self exchangeInstanceMethod1:@selector(deleteSections:) method2:@selector(ym_deleteSections:)];
  150. [self exchangeInstanceMethod1:@selector(reloadSections:) method2:@selector(ym_reloadSections:)];
  151. ///item
  152. [self exchangeInstanceMethod1:@selector(insertItemsAtIndexPaths:) method2:@selector(ym_insertItemsAtIndexPaths:)];
  153. [self exchangeInstanceMethod1:@selector(deleteItemsAtIndexPaths:) method2:@selector(ym_deleteItemsAtIndexPaths:)];
  154. [self exchangeInstanceMethod1:@selector(reloadItemsAtIndexPaths:) method2:@selector(ym_reloadItemsAtIndexPaths:)];
  155. }
  156. - (void)ym_reloadData{
  157. [self ym_reloadData];
  158. [self getDataAndSet];
  159. }
  160. ///section
  161. - (void)ym_insertSections:(NSIndexSet *)sections{
  162. [self ym_insertSections:sections];
  163. [self getDataAndSet];
  164. }
  165. - (void)ym_deleteSections:(NSIndexSet *)sections{
  166. [self ym_deleteSections:sections];
  167. [self getDataAndSet];
  168. }
  169. - (void)ym_reloadSections:(NSIndexSet *)sections{
  170. [self ym_reloadSections:sections];
  171. [self getDataAndSet];
  172. }
  173. ///item
  174. - (void)ym_insertItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths{
  175. [self ym_insertItemsAtIndexPaths:indexPaths];
  176. [self getDataAndSet];
  177. }
  178. - (void)ym_deleteItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths{
  179. [self ym_deleteItemsAtIndexPaths:indexPaths];
  180. [self getDataAndSet];
  181. }
  182. - (void)ym_reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths{
  183. [self ym_reloadItemsAtIndexPaths:indexPaths];
  184. [self getDataAndSet];
  185. }
  186. @end