财神随手记账

UIScrollView+SREmptyData.m 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // UIScrollView+SREmptyData.m
  3. // SREmptyDataViewDemo
  4. //
  5. // Created by https://github.com/guowilling on 2018/1/11.
  6. // Copyright © 2018年 SR. All rights reserved.
  7. //
  8. #import "UIScrollView+SREmptyData.h"
  9. #import "SREmptyDataView.h"
  10. #import <objc/runtime.h>
  11. @implementation UIScrollView (SREmptyData)
  12. //static const char * kEmptyDataViewKey = "emptyDataViewKey";
  13. //static const void * kEmptyDataViewKey = "emptyDataViewKey";
  14. static const void * kEmptyDataViewKey = &kEmptyDataViewKey;
  15. - (void)setSr_emptyDataView:(SREmptyDataView *)sr_emptyDataView {
  16. objc_setAssociatedObject(self, &kEmptyDataViewKey, sr_emptyDataView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  17. // remove old sr_emptyDataView if exist
  18. for (UIView *view in self.subviews) {
  19. if ([view isKindOfClass:[SREmptyDataView class]]) {
  20. [view removeFromSuperview];
  21. }
  22. }
  23. [self addSubview:sr_emptyDataView];
  24. }
  25. - (SREmptyDataView *)sr_emptyDataView {
  26. return objc_getAssociatedObject(self, &kEmptyDataViewKey);
  27. }
  28. #pragma mark - Private Method
  29. - (void)showOrHideEmptyView {
  30. if (self.sr_emptyDataView.autoManagement) {
  31. if ([self shouldShowEmptyView]) {
  32. [self sr_showEmptyDataView];
  33. } else {
  34. [self sr_hideEmptyDataView];
  35. }
  36. }
  37. }
  38. - (BOOL)shouldShowEmptyView {
  39. BOOL flag = YES;
  40. NSInteger dataCount = 0;
  41. if ([self isKindOfClass:[UITableView class]]) {
  42. UITableView *tableView = (UITableView *)self;
  43. for (NSInteger section = 0; section < tableView.numberOfSections; section++) {
  44. dataCount += [tableView numberOfRowsInSection:section];
  45. }
  46. }
  47. if ([self isKindOfClass:[UICollectionView class]]) {
  48. UICollectionView *collectionView = (UICollectionView *)self;
  49. for (NSInteger section = 0; section < collectionView.numberOfSections; section++) {
  50. dataCount += [collectionView numberOfItemsInSection:section];
  51. }
  52. }
  53. if (dataCount > 0) {
  54. flag = NO;
  55. }
  56. return flag;
  57. }
  58. #pragma mark - Public Method
  59. - (void)sr_showEmptyDataView {
  60. self.sr_emptyDataView.hidden = NO;
  61. [self.sr_emptyDataView.superview layoutSubviews];
  62. [self bringSubviewToFront:self.sr_emptyDataView];
  63. }
  64. - (void)sr_hideEmptyDataView {
  65. self.sr_emptyDataView.hidden = YES;
  66. }
  67. - (void)sr_startLoadingData {
  68. [self sr_hideEmptyDataView];
  69. }
  70. - (void)sr_endLoadingData {
  71. if ([self shouldShowEmptyView]) {
  72. [self sr_showEmptyDataView];
  73. } else {
  74. [self sr_hideEmptyDataView];
  75. }
  76. }
  77. @end
  78. @implementation UITableView (Empty)
  79. + (void)load {
  80. }
  81. - (void)sr_reloadData {
  82. [self sr_reloadData];
  83. [self showOrHideEmptyView];
  84. }
  85. - (void)sr_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
  86. [self sr_insertSections:sections withRowAnimation:animation];
  87. [self showOrHideEmptyView];
  88. }
  89. - (void)sr_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
  90. [self sr_insertSections:sections withRowAnimation:animation];
  91. [self showOrHideEmptyView];
  92. }
  93. - (void)sr_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
  94. [self sr_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
  95. [self showOrHideEmptyView];
  96. }
  97. - (void)sr_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
  98. [self sr_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
  99. [self showOrHideEmptyView];
  100. }
  101. @end
  102. @implementation UICollectionView (Empty)
  103. + (void)load {
  104. }
  105. - (void)sr_reloadData {
  106. [self sr_reloadData];
  107. [self showOrHideEmptyView];
  108. }
  109. - (void)sr_insertSections:(NSIndexSet *)sections {
  110. [self sr_insertSections:sections];
  111. [self showOrHideEmptyView];
  112. }
  113. - (void)sr_deleteSections:(NSIndexSet *)sections {
  114. [self sr_deleteSections:sections];
  115. [self showOrHideEmptyView];
  116. }
  117. - (void)sr_insertItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
  118. [self sr_insertItemsAtIndexPaths:indexPaths];
  119. [self showOrHideEmptyView];
  120. }
  121. - (void)sr_deleteItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
  122. [self sr_deleteItemsAtIndexPaths:indexPaths];
  123. [self showOrHideEmptyView];
  124. }
  125. @end