TableRefreshViewController.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // TableRefreshViewController.m
  3. // CommonLibrary
  4. //
  5. // Created by Alexi on 15-2-5.
  6. // Copyright (c) 2015年 Alexi Chen. All rights reserved.
  7. //
  8. #import "TableRefreshViewController.h"
  9. #import "RefreshView.h"
  10. #import "ImageTitleButton.h"
  11. @implementation RequestPageParamItem
  12. - (instancetype)init
  13. {
  14. if (self = [super init])
  15. {
  16. _pageIndex = 0;
  17. _pageSize = 20;
  18. _canLoadMore = YES;
  19. }
  20. return self;
  21. }
  22. - (NSDictionary *)serializeSelfPropertyToJsonObject
  23. {
  24. return @{@"pageIndex" : @(_pageIndex), @"pageSize" : @(_pageSize)};
  25. }
  26. @end
  27. @implementation TableRefreshViewController
  28. - (void)initialize
  29. {
  30. [super initialize];
  31. _clearsSelectionOnViewWillAppear = YES;
  32. _pageItem = [[RequestPageParamItem alloc] init];
  33. }
  34. - (void)addHeaderView
  35. {
  36. self.headerView = [[HeadRefreshView alloc] init];
  37. }
  38. - (void)pinHeaderAndRefesh
  39. {
  40. [self pinHeaderView];
  41. [self refresh];
  42. }
  43. - (void)addFooterView
  44. {
  45. self.footerView = [[FootRefreshView alloc] init];
  46. }
  47. - (void)addRefreshScrollView
  48. {
  49. _tableView = [[UITableView alloc] init];
  50. _tableView.frame = self.view.bounds;
  51. _tableView.dataSource = self;
  52. _tableView.delegate = self;
  53. _tableView.backgroundColor = kClearColor;
  54. _tableView.scrollsToTop = YES;
  55. [self.view addSubview:_tableView];
  56. UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
  57. [_tableView setTableFooterView:v];
  58. _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  59. self.refreshScrollView = _tableView;
  60. }
  61. - (void)viewWillAppear:(BOOL)animated
  62. {
  63. [super viewWillAppear:animated];
  64. if (_tableView)
  65. {
  66. NSIndexPath *selected = [_tableView indexPathForSelectedRow];
  67. if (selected)
  68. {
  69. [_tableView deselectRowAtIndexPath:selected animated:animated];
  70. }
  71. }
  72. }
  73. #pragma mark - UITableViewDelegate
  74. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  75. {
  76. return _datas.count;
  77. }
  78. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  79. {
  80. return nil;
  81. }
  82. - (BOOL)hasData
  83. {
  84. BOOL has = _datas.count != 0;
  85. _tableView.separatorStyle = has ? UITableViewCellSeparatorStyleSingleLine : UITableViewCellSeparatorStyleNone;
  86. return has;
  87. }
  88. - (void)addNoDataView
  89. {
  90. ImageTitleButton *btn = [[ImageTitleButton alloc] initWithStyle:EImageLeftTitleRightCenter];
  91. [btn setImage:[UIImage imageNamed:@"icon_warm"] forState:UIControlStateNormal];
  92. [btn setTitleColor:kDarkGrayColor forState:UIControlStateNormal];
  93. btn.titleLabel.font = kAppMiddleTextFont;
  94. btn.enabled = NO;
  95. btn.titleLabel.textAlignment = NSTextAlignmentCenter;
  96. [self.view addSubview:btn];
  97. _noDataView = btn;
  98. _noDataView.hidden = YES;
  99. }
  100. - (BOOL)needFollowScrollView
  101. {
  102. return NO;
  103. }
  104. - (void)reloadData
  105. {
  106. // BOOL has = [self hasData];
  107. // _noDataView.hidden = has;
  108. // if (!has)
  109. // {
  110. // [self showNoDataView];
  111. // }
  112. [_tableView reloadData];
  113. [self allLoadingCompleted];
  114. // if ([self needFollowScrollView])
  115. // {
  116. // if (_tableView.contentSize.height > 2 * _tableView.bounds.size.height)
  117. // {
  118. // [self followScrollView:_tableView];
  119. // }
  120. // else
  121. // {
  122. // [self followScrollView:nil];
  123. // }
  124. // }
  125. }
  126. - (void)showNoDataView
  127. {
  128. }
  129. - (void)allLoadingCompleted
  130. {
  131. [super allLoadingCompleted];
  132. BOOL has = [self hasData];
  133. _noDataView.hidden = has;
  134. if (!has)
  135. {
  136. [self showNoDataView];
  137. }
  138. }
  139. @end