GHRefreshTableView.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. //
  2. // GHRefreshTableView.m
  3. // GameHelper
  4. //
  5. // Created by 青秀斌 on 16/12/29.
  6. // Copyright © 2016年 kylincc. All rights reserved.
  7. //
  8. #import "GHRefreshTableView.h"
  9. #import "GHRefreshHeader.h"
  10. #import "GHRefreshFooter.h"
  11. #import "UIView+GHBlank.h"
  12. #import "AFNetworkReachabilityManager.h"
  13. #import "UITableView+HeadSet.h"
  14. #import <Masonry.h>
  15. #define NET_CONNECT_ERROR @"当前网络不给力,请稍后重试"
  16. @interface GHRefreshTableView ()
  17. @property (strong, nonatomic) NSMutableArray *dataArray;
  18. @property (assign, nonatomic) NSUInteger pageIndex;
  19. @property (assign, nonatomic) NSUInteger offset;
  20. @end
  21. @implementation GHRefreshTableView
  22. - (instancetype)init {
  23. if (self = [super init]) {
  24. [self awakeFromNib];
  25. }
  26. return self;
  27. }
  28. - (instancetype)initWithFrame:(CGRect)frame {
  29. if (self = [super initWithFrame:frame]) {
  30. [self awakeFromNib];
  31. }
  32. return self;
  33. }
  34. - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
  35. if (self = [super initWithFrame:frame style:style]) {
  36. [self awakeFromNib];
  37. }
  38. return self;
  39. }
  40. - (void)awakeFromNib {
  41. [super awakeFromNib];
  42. self.pageIndex = 1;
  43. self.offset = 0;
  44. self.pageSize = 20;
  45. self.allowShowMore = YES;
  46. self.allowShowBlank = YES;
  47. self.allowShowNoNetworkBlank = YES;
  48. self.allowExternalControlShowBlank = NO;
  49. self.allowShowBlankWithHeaderTableView = NO;
  50. self.blankImage = nil;
  51. self.blankTitle = nil;
  52. self.blankMessage = nil;
  53. self.offsetY = 0;
  54. self.carRefreshBackgroundType = kCarRefreshBackgroundTypeDoubleCircle;//下拉加载动画
  55. }
  56. - (void)setRefreshDelegate:(id<GHRefreshTableViewDelegate>)refreshDelegate {
  57. _refreshDelegate = refreshDelegate;
  58. if ([self.refreshDelegate respondsToSelector:@selector(tableView:pageIndex:offset:pageSize:success:failure:)]) {
  59. [self addRefreshHeader];
  60. }
  61. }
  62. - (void)setAllowShowMore:(BOOL)allowShowMore {
  63. _allowShowMore = allowShowMore;
  64. if (self.mj_footer) {
  65. [self.mj_footer removeFromSuperview];
  66. self.mj_footer = nil;
  67. }
  68. }
  69. -(void)removeHeaderView{
  70. if (self.mj_header) {
  71. [self.mj_header removeFromSuperview];
  72. self.mj_header = nil;
  73. }
  74. if (self.refreshHeaderView) {
  75. [self.refreshHeaderView removeFromSuperview];
  76. [self setRefreshHeaderView:nil];
  77. }
  78. }
  79. /**********************************************************************/
  80. #pragma mark - Private
  81. /**********************************************************************/
  82. - (void)addRefreshHeader {
  83. if (self.mj_header) {
  84. return;
  85. }
  86. GHRefreshHeader *header = [GHRefreshHeader headerWithRefreshingBlock:^{
  87. NSUInteger pageIndex = 1;
  88. NSUInteger offset = 0;
  89. WS(weakSelf);
  90. [self.refreshDelegate tableView:self pageIndex:pageIndex offset:offset pageSize:self->_pageSize success:^(NSArray *list, BOOL hasNext) {
  91. [weakSelf.mj_header endRefreshing];
  92. //添加RefreshFooter
  93. [weakSelf addRefreshFooter];
  94. if (hasNext) {
  95. [weakSelf.mj_footer endRefreshing];
  96. } else {
  97. [weakSelf.mj_footer endRefreshingWithNoMoreData];
  98. }
  99. //加载数据
  100. weakSelf.pageIndex = pageIndex;
  101. weakSelf.offset = offset;
  102. if (weakSelf.reloadView) {
  103. weakSelf.reloadView(list);
  104. } else {
  105. weakSelf.dataArray = [list mutableCopy];
  106. [weakSelf reloadData];
  107. }
  108. //显示占位图
  109. if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) {
  110. weakSelf.mj_footer.hidden = YES;
  111. if(self.allowShowBlankWithHeaderTableView){
  112. [weakSelf addHeaderView:weakSelf.blankImage?:@"no_data" title:weakSelf.blankTitle?:@"暂无数据"];
  113. }else{
  114. [weakSelf showBlankWithImage:weakSelf.blankImage?:@"no_data"
  115. title:weakSelf.blankTitle?:@"暂无数据"
  116. message:weakSelf.blankMessage
  117. action:nil offsetY:weakSelf.offsetY];
  118. }
  119. } else {
  120. [weakSelf dismissBlank];
  121. if(!weakSelf.allowExternalControlShowBlank){
  122. weakSelf.mj_footer.hidden = NO;
  123. [weakSelf setHeadIsNil];
  124. }else{
  125. weakSelf.mj_footer.hidden = YES;
  126. }
  127. }
  128. } failure:^(NSError *error) {
  129. [weakSelf.mj_header endRefreshing];
  130. //显示占位图
  131. NSString *blankTitle = error.localizedDescription?:weakSelf.blankTitle;
  132. if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) {
  133. weakSelf.mj_footer.hidden = YES;
  134. BOOL isHaveNet = NO;
  135. if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
  136. isHaveNet = YES;
  137. }
  138. if (weakSelf.allowShowNoNetworkBlank && isHaveNet) {
  139. if(self.allowShowBlankWithHeaderTableView){
  140. [weakSelf addHeaderView:@"no_network" title:NET_CONNECT_ERROR];
  141. }else{
  142. [weakSelf showBlankWithImage:@"no_network"
  143. title:NET_CONNECT_ERROR
  144. message:nil
  145. action:nil offsetY:weakSelf.offsetY];
  146. }
  147. } else {
  148. if(self.allowShowBlankWithHeaderTableView){
  149. [weakSelf addHeaderView:weakSelf.blankImage?:@"failed_to_load" title:blankTitle?:@"加载失败"];
  150. }else{
  151. [weakSelf showBlankWithImage:weakSelf.blankImage?:@"failed_to_load"
  152. title:blankTitle?:@"加载失败"
  153. message:weakSelf.blankMessage
  154. action:nil offsetY:weakSelf.offsetY];
  155. }
  156. }
  157. } else {
  158. [weakSelf dismissBlank];
  159. if(!weakSelf.allowExternalControlShowBlank){
  160. weakSelf.mj_footer.hidden = NO;
  161. [weakSelf setHeadIsNil];
  162. }else{
  163. weakSelf.mj_footer.hidden = YES;
  164. }
  165. // [ShowtipTool showErrorWithStatus:blankTitle];
  166. }
  167. }];
  168. }carRefreshBackgroundType:self.carRefreshBackgroundType];
  169. self.refreshHeaderView = header;
  170. self.mj_header = header;
  171. }
  172. - (void)addRefreshFooter {
  173. if (!self.allowShowMore) {
  174. return;
  175. }
  176. if (self.mj_footer) {
  177. return;
  178. }
  179. GHRefreshFooter *footer = [GHRefreshFooter footerWithRefreshingBlock:^{
  180. NSUInteger pageIndex = self.pageIndex + 1;
  181. NSInteger offset = self.dataArray.count + 1;
  182. WS(weakSelf);
  183. [self.refreshDelegate tableView:self pageIndex:pageIndex offset:offset pageSize:_pageSize success:^(NSArray * _Nullable list, BOOL hasNext) {
  184. if (hasNext) {
  185. [weakSelf.mj_footer endRefreshing];
  186. } else {
  187. [weakSelf.mj_footer endRefreshingWithNoMoreData];
  188. }
  189. //加载数据
  190. weakSelf.pageIndex = pageIndex;
  191. weakSelf.offset = offset;
  192. if (weakSelf.reloadView) {
  193. weakSelf.reloadView(list);
  194. } else {
  195. [weakSelf.dataArray addObjectsFromArray:list];
  196. [weakSelf reloadData];
  197. }
  198. //显示占位图
  199. if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) {
  200. weakSelf.mj_footer.hidden = YES;
  201. if(self.allowShowBlankWithHeaderTableView){
  202. [weakSelf addHeaderView:weakSelf.blankImage?:@"no_data" title:weakSelf.blankTitle?:@"暂无数据"];
  203. }else{
  204. [weakSelf showBlankWithImage:weakSelf.blankImage?:@"no_data"
  205. title:weakSelf.blankTitle?:@"暂无数据"
  206. message:weakSelf.blankMessage
  207. action:nil offsetY:weakSelf.offsetY];
  208. }
  209. } else {
  210. [weakSelf dismissBlank];
  211. if(!weakSelf.allowExternalControlShowBlank){
  212. weakSelf.mj_footer.hidden = NO;
  213. [weakSelf setHeadIsNil];
  214. }else{
  215. weakSelf.mj_footer.hidden = YES;
  216. }
  217. }
  218. } failure:^(NSError *error) {
  219. [weakSelf.mj_footer endRefreshing];
  220. //显示占位图
  221. NSString *blankTitle = error.localizedDescription?:weakSelf.blankTitle;
  222. if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) {
  223. weakSelf.mj_footer.hidden = YES;
  224. BOOL isHaveNet = NO;
  225. if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
  226. isHaveNet = YES;
  227. }
  228. if (weakSelf.allowShowNoNetworkBlank && isHaveNet) {
  229. if(self.allowShowBlankWithHeaderTableView){
  230. [weakSelf addHeaderView:@"no_network" title:NET_CONNECT_ERROR];
  231. }else{
  232. [weakSelf showBlankWithImage:@"no_network"
  233. title:NET_CONNECT_ERROR
  234. message:nil
  235. action:nil offsetY:weakSelf.offsetY];
  236. }
  237. } else {
  238. if(self.allowShowBlankWithHeaderTableView){
  239. [weakSelf addHeaderView:weakSelf.blankImage?:@"failed_to_load" title:blankTitle?:@"加载失败"];
  240. }else{
  241. [weakSelf showBlankWithImage:weakSelf.blankImage?:@"failed_to_load"
  242. title:blankTitle?:@"加载失败"
  243. message:weakSelf.blankMessage
  244. action:nil offsetY:weakSelf.offsetY];
  245. }
  246. }
  247. } else {
  248. [weakSelf dismissBlank];
  249. if(!weakSelf.allowExternalControlShowBlank){
  250. weakSelf.mj_footer.hidden = NO;
  251. [weakSelf setHeadIsNil];
  252. }else{
  253. weakSelf.mj_footer.hidden = YES;
  254. }
  255. // [ShowtipTool showErrorWithStatus:blankTitle];
  256. }
  257. }];
  258. }];
  259. self.mj_footer = footer;
  260. }
  261. /**********************************************************************/
  262. #pragma mark - Public
  263. /**********************************************************************/
  264. - (void)refreshData {
  265. [self.mj_header beginRefreshing];
  266. }
  267. - (void)refreshDataNoAnimation{
  268. NSUInteger pageIndex = 1;
  269. NSInteger offset = 0;
  270. WS(weakSelf);
  271. [self.refreshDelegate tableView:self pageIndex:pageIndex offset:offset pageSize:_pageSize success:^(NSArray *list, BOOL hasNext) {
  272. [weakSelf.mj_header endRefreshing];
  273. //添加RefreshFooter
  274. [weakSelf addRefreshFooter];
  275. if (hasNext) {
  276. [weakSelf.mj_footer endRefreshing];
  277. } else {
  278. [weakSelf.mj_footer endRefreshingWithNoMoreData];
  279. }
  280. //加载数据
  281. weakSelf.pageIndex = pageIndex;
  282. weakSelf.offset = offset;
  283. if (weakSelf.reloadView) {
  284. weakSelf.reloadView(list);
  285. } else {
  286. weakSelf.dataArray = [list mutableCopy];
  287. [weakSelf reloadData];
  288. }
  289. //显示占位图
  290. if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) {
  291. weakSelf.mj_footer.hidden = YES;
  292. if(self.allowShowBlankWithHeaderTableView){
  293. [weakSelf addHeaderView:weakSelf.blankImage?:@"no_data" title:weakSelf.blankTitle?:@"暂无数据"];
  294. }else{
  295. [weakSelf showBlankWithImage:weakSelf.blankImage?:@"no_data"
  296. title:weakSelf.blankTitle?:@"暂无数据"
  297. message:weakSelf.blankMessage
  298. action:nil offsetY:weakSelf.offsetY];
  299. }
  300. } else {
  301. [weakSelf dismissBlank];
  302. if(!weakSelf.allowExternalControlShowBlank){
  303. weakSelf.mj_footer.hidden = NO;
  304. [weakSelf setHeadIsNil];
  305. }else{
  306. weakSelf.mj_footer.hidden = YES;
  307. }
  308. }
  309. } failure:^(NSError *error) {
  310. [weakSelf.mj_header endRefreshing];
  311. //显示占位图
  312. NSString *blankTitle = error.localizedDescription?:weakSelf.blankTitle;
  313. if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) {
  314. weakSelf.mj_footer.hidden = YES;
  315. BOOL isHaveNet = NO;
  316. if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
  317. isHaveNet = YES;
  318. }
  319. if (weakSelf.allowShowNoNetworkBlank && isHaveNet) {
  320. if(self.allowShowBlankWithHeaderTableView){
  321. [weakSelf addHeaderView:@"no_network" title:NET_CONNECT_ERROR];
  322. }else{
  323. [weakSelf showBlankWithImage:@"no_network"
  324. title:NET_CONNECT_ERROR
  325. message:nil
  326. action:nil offsetY:weakSelf.offsetY];
  327. }
  328. } else {
  329. if(self.allowShowBlankWithHeaderTableView){
  330. [weakSelf addHeaderView:weakSelf.blankImage?:@"failed_to_load" title:blankTitle?:@"加载失败"];
  331. }else{
  332. [weakSelf showBlankWithImage:weakSelf.blankImage?:@"failed_to_load"
  333. title:blankTitle?:@"加载失败"
  334. message:weakSelf.blankMessage
  335. action:nil offsetY:weakSelf.offsetY];
  336. }
  337. }
  338. } else {
  339. [weakSelf dismissBlank];
  340. if(!weakSelf.allowExternalControlShowBlank){
  341. weakSelf.mj_footer.hidden = NO;
  342. [weakSelf setHeadIsNil];
  343. }else{
  344. weakSelf.mj_footer.hidden = YES;
  345. }
  346. // [ShowtipTool showErrorWithStatus:blankTitle];
  347. }
  348. }];
  349. }
  350. //针对学习模块 - 删除所有数据之后再进行刷新
  351. - (void)removeAllDataToReload{
  352. self.mj_footer.hidden = YES;
  353. if(self.allowShowBlankWithHeaderTableView){
  354. [self addHeaderView:self.blankImage?:@"no_data" title:self.blankTitle?:@"暂无数据"];
  355. }else{
  356. [self showBlankWithImage:self.blankImage?:@"no_data"
  357. title:self.blankTitle?:@"暂无数据"
  358. message:self.blankMessage
  359. action:nil offsetY:self.offsetY];
  360. }
  361. }
  362. - (void)showBlankView:(NSString *)blankImg blankTitle:(NSString *)blankTitle{
  363. [self.mj_header endRefreshing];
  364. [self.mj_footer endRefreshing];
  365. self.mj_footer.hidden = YES;
  366. BOOL isHaveNet = NO;
  367. if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
  368. isHaveNet = YES;
  369. }
  370. if (isHaveNet) {
  371. blankImg = @"no_network";
  372. blankTitle = NET_CONNECT_ERROR;
  373. }
  374. if(self.allowShowBlankWithHeaderTableView){
  375. [self addHeaderView:blankImg title:blankTitle];
  376. }else{
  377. [self showBlankWithImage:blankImg
  378. title:blankTitle
  379. message:self.blankMessage
  380. action:nil offsetY:self.offsetY];
  381. }
  382. }
  383. -(void)addHeaderView:(NSString *)blankImg title:(NSString *)blankTitle{
  384. if(self.allowShowBlankWithHeaderTableViewByNotDeveloped){
  385. [self setHeadImageAndTextWithNotDeveloped:[UIImage imageNamed:@"will_open"]];
  386. }else{
  387. [self setHeadImageAndText:[UIImage imageNamed:blankImg] text:blankTitle];
  388. }
  389. }
  390. #pragma mark 移除“没有数据”文字+图片
  391. -(void)removeBlackView{
  392. [self dismissBlank];
  393. self.tableFooterView = nil;
  394. }
  395. #pragma mark 重新设置BlankView的位置
  396. -(void)showBlankView_offsetY:(CGFloat)offsetY{
  397. self.tableFooterView = nil;
  398. if(self.dataArray.count == 0){
  399. offsetY = offsetY/2;
  400. [self dismissBlank];
  401. UIView *fv = [[UIView alloc] init];
  402. fv.backgroundColor = [UIColor whiteColor];
  403. self.tableFooterView = fv;
  404. fv.height = 300;
  405. UIImageView *blankImageV = [[UIImageView alloc] init];
  406. [fv addSubview:blankImageV];
  407. UILabel *blankLabel = [[UILabel alloc] init];
  408. [fv addSubview:blankLabel];
  409. blankLabel.font = [UIFont boldSystemFontOfSize:11];
  410. blankLabel.textColor = RGB(42, 42, 42);
  411. blankImageV.image = [UIImage imageNamed:self.blankImage?:@"no_data"];
  412. blankLabel.text = self.blankTitle?:@"暂无数据";
  413. [blankImageV mas_makeConstraints:^(MASConstraintMaker *make) {
  414. make.top.equalTo(fv).offset(50);
  415. make.centerX.equalTo(fv);
  416. }];
  417. [blankLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  418. make.centerX.equalTo(fv);
  419. make.bottom.equalTo(blankImageV).offset(20);
  420. }];
  421. [self reloadData];
  422. }
  423. }
  424. @end