dkahgld

UITableView+FMListPlaceholder.m 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // UITableView+FMListPlaceholder.m
  3. // FMListPlaceholderExample
  4. //
  5. // Created by Mingo on 2018/8/9.
  6. // Copyright © 2017年 袁凤鸣. All rights reserved.
  7. // 项目地址:https://github.com/yfming93/FMListPlaceholder
  8. // 作者博客:https://www.yfmingo.cn
  9. //
  10. #import "UITableView+FMListPlaceholder.h"
  11. #import "NSObject+FMSwizzling.h"
  12. #import "FMPlaceholderView.h"
  13. @interface UITableView ()
  14. @property (nonatomic, assign) BOOL isSecondReloadData;
  15. @property (nonatomic, strong) FMPlaceholderView *placeholderView;
  16. @end
  17. @implementation UITableView (FMListPlaceholder)
  18. + (void)load {
  19. static dispatch_once_t onceToken;
  20. dispatch_once(&onceToken, ^{
  21. [self fm_methodSwizzlingWithOriginalSelector:@selector(reloadData)
  22. bySwizzledSelector:@selector(sure_reloadData)];
  23. });
  24. }
  25. static void *kFMPlaceholderView = &kFMPlaceholderView;
  26. - (FMPlaceholderView *)placeholderView {
  27. FMPlaceholderView *placeholderView = objc_getAssociatedObject(self, kFMPlaceholderView);
  28. if (!placeholderView) {
  29. placeholderView = [[FMPlaceholderView alloc]initWithFrame:CGRectMake(0, self.tableHeaderView.frame.size.height, self.frame.size.width, self.frame.size.height -self.tableHeaderView.frame.size.height - self.tableFooterView.frame.size.height)];
  30. __weak typeof(self) weakSelf = self;
  31. placeholderView.reloadClickBlock = ^{
  32. if (weakSelf.reloadBlock) {
  33. weakSelf.reloadBlock(self);
  34. }
  35. };
  36. objc_setAssociatedObject(self, kFMPlaceholderView, placeholderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  37. }
  38. return placeholderView;
  39. }
  40. - (void)setPlaceholderView:(UIView *)placeholderView {
  41. objc_setAssociatedObject(self, @selector(placeholderView), placeholderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  42. }
  43. - (void)sure_reloadData {
  44. [self addSubview:self.placeholderView];
  45. [self sure_reloadData]; //因为转换过方法。这里调用 sure_reloadData 就是调用 reloadData
  46. if (self.isSecondReloadData) {
  47. if (self.needPlaceholderView ) {
  48. self.placeholderView.userInteractionEnabled = NO;
  49. [self fm_relaodPlaceholderViewHiddenOrShow];
  50. }
  51. if ( self.reloadBlock) {
  52. self.placeholderView.userInteractionEnabled = YES;
  53. [self fm_relaodPlaceholderViewHiddenOrShow];
  54. }
  55. }else{
  56. self.isSecondReloadData = YES ;
  57. }
  58. }
  59. - (void)fm_relaodPlaceholderViewHiddenOrShow {
  60. BOOL isEmpty = [self isEmptyDataCheck];
  61. if (isEmpty) {//若为空,加载占位图
  62. self.placeholderView.hidden = NO;
  63. }else{
  64. self.placeholderView.hidden = YES;
  65. }
  66. }
  67. - (BOOL)isEmptyDataCheck {
  68. BOOL isEmpty = YES;
  69. id <UITableViewDataSource> dataSource = self.dataSource;
  70. NSInteger sections = 1;
  71. if ([dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
  72. sections = [dataSource numberOfSectionsInTableView:self] ;//获取当前TableView组数
  73. }
  74. for (NSInteger i = 0; i < sections; i++) {
  75. NSInteger rows = [dataSource tableView:self numberOfRowsInSection:i];//获取当前TableView各组行数
  76. if (rows) {
  77. isEmpty = NO;//若行数存在,不为空
  78. }
  79. }
  80. return isEmpty;
  81. }
  82. - (void)fm_coverCenterYOffset:(CGFloat)coverCenterYOffset coverSize:(CGSize)coverSize coverSpaceToTips:(CGFloat)coverSpaceToTips {
  83. [self fm_emptyCoverName:nil tips:nil backgroundColor:nil tipsTextColor:nil tipsFont:nil coverCenterYOffset:@(coverCenterYOffset) coverSize:coverSize coverSpaceToTips:@(coverSpaceToTips) ];
  84. }
  85. - (void)fm_backgroundColor:(UIColor *)backgroundColor tipsTextColor:(UIColor *)tipsTextColor tipsFont:(UIFont *)tipsFont {
  86. [self fm_emptyCoverName:nil tips:nil backgroundColor:backgroundColor tipsTextColor:tipsTextColor tipsFont:tipsFont coverCenterYOffset:nil coverSize:CGSizeZero coverSpaceToTips:nil ];
  87. }
  88. - (void)fm_emptyCoverName:(NSString *)emptyCoverName emptyTips:(NSString *)emptyTips {
  89. [self fm_emptyCoverName:emptyCoverName tips:emptyTips backgroundColor:nil tipsTextColor:nil tipsFont:nil coverCenterYOffset:nil coverSize:CGSizeZero coverSpaceToTips:nil ];
  90. }
  91. - (void)fm_defaultPlaceholder:(UIView *)defaultPlaceholder {
  92. if (defaultPlaceholder != nil) {
  93. self.placeholderView.defaultPlaceholder = defaultPlaceholder;
  94. }
  95. }
  96. - (void)fm_emptyCoverName:(NSString *)emptyCoverName tips:(NSString *)tips backgroundColor:(UIColor *)backgroundColor tipsTextColor:(UIColor *)tipsTextColor tipsFont:(UIFont *)tipsFont coverCenterYOffset:(NSNumber *)coverCenterYOffset coverSize:(CGSize)coverSize coverSpaceToTips:(NSNumber *)coverSpaceToTips {
  97. if (emptyCoverName.length) {
  98. self.placeholderView.emptyCover.image = [UIImage imageNamed:emptyCoverName];
  99. }
  100. if (tips.length) {
  101. [self.placeholderView.emptyTips setTitle:tips forState:UIControlStateNormal];
  102. }
  103. if (backgroundColor) {
  104. self.placeholderView.backgroundColor = backgroundColor;
  105. }
  106. if (tipsTextColor) {
  107. [self.placeholderView.emptyTips setTitleColor:tipsTextColor forState:UIControlStateNormal];
  108. }
  109. if (tipsFont) {
  110. [self.placeholderView.emptyTips.titleLabel setFont:tipsFont];;
  111. }
  112. if (coverSpaceToTips != nil) {
  113. self.placeholderView.coverSpaceToTips = coverSpaceToTips.floatValue;
  114. }
  115. if (coverSize.width) {
  116. self.placeholderView.coverSize = coverSize;
  117. }
  118. if (coverCenterYOffset != nil) {
  119. self.placeholderView.coverCenterYOffset = coverCenterYOffset.floatValue;
  120. }
  121. }
  122. - (BOOL)isSecondReloadData {
  123. return [objc_getAssociatedObject(self, @selector(isSecondReloadData)) boolValue];
  124. }
  125. - (void)setIsSecondReloadData:(BOOL)isSecondReloadData {
  126. objc_setAssociatedObject(self, @selector(isSecondReloadData), @(isSecondReloadData), OBJC_ASSOCIATION_ASSIGN);
  127. }
  128. - (BOOL)needPlaceholderView {
  129. return [objc_getAssociatedObject(self, @selector(needPlaceholderView)) boolValue];
  130. }
  131. - (void)setNeedPlaceholderView:(BOOL)needPlaceholderView {
  132. objc_setAssociatedObject(self, @selector(needPlaceholderView), @(needPlaceholderView), OBJC_ASSOCIATION_ASSIGN);
  133. }
  134. - (void (^)(UIScrollView *list))reloadBlock {
  135. return objc_getAssociatedObject(self, @selector(reloadBlock));
  136. }
  137. - (void)setReloadBlock:(void (^)(UIScrollView *list))reloadBlock {
  138. objc_setAssociatedObject(self, @selector(reloadBlock), reloadBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
  139. }
  140. @end