省钱达人

UITableView+AddForPlaceholder.m 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // UITableView+AddForPlaceholder.m
  3. // TableViewPlaceholder
  4. //
  5. // Created by TengShuQiang on 2017/12/4.
  6. // Copyright © 2017年 TTeng. All rights reserved.
  7. //
  8. #import "UITableView+AddForPlaceholder.h"
  9. #import <objc/runtime.h>
  10. @implementation UITableView (AddForPlaceholder)
  11. - (void)dealloc {
  12. [[NSNotificationCenter defaultCenter] removeObserver:self];
  13. }
  14. void swizzMethod(SEL oriSel, SEL newSel) {
  15. Class class = [UITableView class];
  16. Method oriMethod = class_getInstanceMethod(class, oriSel);
  17. Method newMethod = class_getInstanceMethod(class, newSel);
  18. BOOL success = class_addMethod(class, oriSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
  19. if (success) {
  20. class_replaceMethod(class, newSel, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
  21. } else {
  22. method_exchangeImplementations(oriMethod, newMethod);
  23. }
  24. }
  25. + (void)load {
  26. SEL selectors[] = {
  27. @selector(reloadData),
  28. @selector(insertSections:withRowAnimation:),
  29. @selector(deleteSections:withRowAnimation:),
  30. @selector(reloadSections:withRowAnimation:),
  31. @selector(insertRowsAtIndexPaths:withRowAnimation:),
  32. @selector(deleteRowsAtIndexPaths:withRowAnimation:),
  33. @selector(reloadRowsAtIndexPaths:withRowAnimation:),
  34. };
  35. for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
  36. SEL originalSelector = selectors[index];
  37. SEL swizzledSelector = NSSelectorFromString([@"tt_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
  38. swizzMethod(originalSelector, swizzledSelector);
  39. }
  40. }
  41. - (void)tt_reloadData {
  42. [self tt_reloadData];
  43. [self showPlaceholderNotice];
  44. }
  45. - (void)tt_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
  46. [self tt_insertSections:sections withRowAnimation:animation];
  47. [self showPlaceholderNotice];
  48. }
  49. - (void)tt_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
  50. [self tt_deleteSections:sections withRowAnimation:animation];
  51. [self showPlaceholderNotice];
  52. }
  53. - (void)tt_reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
  54. [self tt_reloadSections:sections withRowAnimation:animation];
  55. [self showPlaceholderNotice];
  56. }
  57. - (void)tt_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
  58. [self tt_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
  59. [self showPlaceholderNotice];
  60. }
  61. - (void)tt_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
  62. [self tt_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
  63. [self showPlaceholderNotice];
  64. }
  65. - (void)tt_reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
  66. [self tt_reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
  67. [self showPlaceholderNotice];
  68. }
  69. - (void)showPlaceholderNotice {
  70. if (self.showNoDataNotice) {
  71. NSInteger sectionCount = self.numberOfSections;
  72. NSInteger rowCount = 0;
  73. for (int i = 0; i < sectionCount; i++) {
  74. rowCount += [self.dataSource tableView:self numberOfRowsInSection:i];
  75. }
  76. if (rowCount == 0) {
  77. if (self.showNoDataView) {
  78. if (self.customNoDataView) {
  79. self.backgroundView = [self customNoDataView];
  80. } else
  81. self.backgroundView = [self tt_defaultNoDataView];
  82. }
  83. } else {
  84. self.backgroundView = [[UIView alloc] init];
  85. }
  86. }
  87. }
  88. - (UIView *)tt_defaultNoDataView {
  89. if (self.defaultNoDataView) {
  90. return self.defaultNoDataView;
  91. }
  92. self.defaultNoDataView = ({
  93. UIView *view = [[UIView alloc] initWithFrame:self.bounds];
  94. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tt_tapDefalutNoDataView:)];
  95. [view addGestureRecognizer:tap];
  96. [view addSubview:self.defaultNoDataNoticeImageView];
  97. [view addSubview:self.defaultNoDataNoticeLabel];
  98. [self layoutDefaultView:view];
  99. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDeviceOrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
  100. view;
  101. });
  102. return self.defaultNoDataView;
  103. }
  104. - (void)layoutDefaultView:(UIView *)defaultView {
  105. UIImageView *imageView = self.defaultNoDataNoticeImageView;
  106. UIImage *image = self.defaultNoDataImage ? : [UIImage imageNamed:@"UITableViewPlaceholder.bundle/TableViewNoData"];
  107. imageView.image = image;
  108. CGFloat X = (self.bounds.size.width - image.size.width - self.contentInset.left - self.contentInset.right) / 2;
  109. CGFloat Y = (self.bounds.size.height - image.size.height - self.contentInset.top - self.contentInset.bottom) / 2 - 20;
  110. imageView.frame = CGRectMake(X, Y+self.noDataImageOffsetY, image.size.width, image.size.height);
  111. // 提示语不用太长,不考虑换行的情况,也不计算文字的宽高了
  112. UILabel *label = self.defaultNoDataNoticeLabel;
  113. label.text = self.defaultNoDataText ? : label.text;
  114. label.frame = CGRectMake(0, imageView.frame.origin.y + imageView.bounds.size.height + 10, self.bounds.size.width, 30);
  115. }
  116. - (void)tt_tapDefalutNoDataView:(UITapGestureRecognizer *)tap {
  117. self.defaultNoDataViewDidClickBlock ? self.defaultNoDataViewDidClickBlock(self.defaultNoDataView) : nil;
  118. }
  119. #pragma mark - notifications
  120. - (void)onDeviceOrientationChange:(NSNotification *)noti {
  121. if (self.customNoDataView || !self.showNoDataNotice) {
  122. return;
  123. }
  124. [self layoutDefaultView:self.defaultNoDataView];
  125. }
  126. #pragma mark - setter && getter
  127. - (void)setShowNoDataNotice:(BOOL)showNoDataNotice {
  128. objc_setAssociatedObject(self, @selector(showNoDataNotice), @(showNoDataNotice), OBJC_ASSOCIATION_ASSIGN);
  129. }
  130. - (BOOL)showNoDataNotice {
  131. return objc_getAssociatedObject(self, _cmd) == nil ? YES : [objc_getAssociatedObject(self, _cmd) boolValue];
  132. }
  133. - (void)setDefaultNoDataViewDidClickBlock:(void (^)(UIView *))defaultNoDataViewDidClickBlock {
  134. self.showNoDataNotice = YES;
  135. objc_setAssociatedObject(self, @selector(defaultNoDataViewDidClickBlock), defaultNoDataViewDidClickBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
  136. }
  137. - (void (^)(UIView *))defaultNoDataViewDidClickBlock {
  138. return objc_getAssociatedObject(self, _cmd);
  139. }
  140. - (void)setCustomNoDataView:(UIView *)customNoDataView {
  141. self.showNoDataNotice = YES;
  142. objc_setAssociatedObject(self, @selector(customNoDataView), customNoDataView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  143. }
  144. - (UIView *)customNoDataView {
  145. return objc_getAssociatedObject(self, _cmd);
  146. }
  147. - (UIView *)defaultNoDataView {
  148. return objc_getAssociatedObject(self, _cmd);
  149. }
  150. - (void)setDefaultNoDataView:(UIView *)defaultNoDataView {
  151. objc_setAssociatedObject(self, @selector(defaultNoDataView), defaultNoDataView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  152. }
  153. // 默认的label
  154. - (UILabel *)defaultNoDataNoticeLabel {
  155. UILabel *label = objc_getAssociatedObject(self, _cmd);
  156. if (!label) {
  157. label = [[UILabel alloc] init];
  158. label.text = self.defaultNoDataText ? : @"暂无数据,点击刷新";
  159. label.font = [UIFont systemFontOfSize:13];
  160. label.textColor = [UIColor lightGrayColor];
  161. label.textAlignment = NSTextAlignmentCenter;
  162. objc_setAssociatedObject(self, _cmd, label, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  163. }
  164. return label;
  165. }
  166. // 默认的imageView
  167. - (UIImageView *)defaultNoDataNoticeImageView {
  168. UIImageView *imageView = objc_getAssociatedObject(self, _cmd);
  169. if (!imageView) {
  170. imageView = [[UIImageView alloc] init];
  171. imageView.image = self.defaultNoDataImage ? : [UIImage imageNamed:@"UITableViewPlaceholder.bundle/TableViewNoData"];
  172. imageView.contentMode = UIViewContentModeCenter;
  173. objc_setAssociatedObject(self, _cmd, imageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  174. }
  175. return imageView;
  176. }
  177. - (NSString *)defaultNoDataText {
  178. return objc_getAssociatedObject(self, _cmd);
  179. }
  180. - (void)setDefaultNoDataText:(NSString *)defaultNoticeText {
  181. objc_setAssociatedObject(self, @selector(defaultNoDataText), defaultNoticeText, OBJC_ASSOCIATION_COPY_NONATOMIC);
  182. }
  183. - (UIImage *)defaultNoDataImage {
  184. return objc_getAssociatedObject(self, _cmd);
  185. }
  186. - (void)setDefaultNoDataImage:(UIImage *)defaultNoticeImage {
  187. objc_setAssociatedObject(self, @selector(defaultNoDataImage), defaultNoticeImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  188. }
  189. - (BOOL)showNoDataView {
  190. NSNumber *value = objc_getAssociatedObject(self, @selector(showNoDataView));
  191. return value.boolValue;
  192. }
  193. - (void)setShowNoDataView:(BOOL)showNoDataView {
  194. objc_setAssociatedObject(self, @selector(showNoDataView), @(showNoDataView), OBJC_ASSOCIATION_ASSIGN);
  195. }
  196. - (CGFloat)noDataImageOffsetY {
  197. NSNumber *value = objc_getAssociatedObject(self, @selector(noDataImageOffsetY));
  198. return value.floatValue;
  199. }
  200. - (void)setNoDataImageOffsetY:(CGFloat)noDataImageOffsetY {
  201. objc_setAssociatedObject(self, @selector(noDataImageOffsetY), @(noDataImageOffsetY), OBJC_ASSOCIATION_ASSIGN);
  202. }
  203. @end