JXTableView.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #import "JXTableView.h"
  2. @implementation JXTableView
  3. @synthesize touchDelegate = _touchDelegate;
  4. - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
  5. self = [super initWithFrame:frame style:style];
  6. _pool = [[NSMutableArray alloc]init];
  7. return self;
  8. }
  9. -(void)dealloc{
  10. NSLog(@"JXTableView.dealloc");
  11. [self clearPool];
  12. _pool = nil;
  13. // [super dealloc];
  14. }
  15. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  16. {
  17. [super touchesBegan:touches withEvent:event];
  18. if ([_touchDelegate conformsToProtocol:@protocol(JXTableViewDelegate)] &&
  19. [_touchDelegate respondsToSelector:@selector(tableView:touchesBegan:withEvent:)])
  20. {
  21. [_touchDelegate tableView:self touchesBegan:touches withEvent:event];
  22. }
  23. }
  24. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  25. {
  26. [super touchesCancelled:touches withEvent:event];
  27. if ([_touchDelegate conformsToProtocol:@protocol(JXTableViewDelegate)] &&
  28. [_touchDelegate respondsToSelector:@selector(tableView:touchesCancelled:withEvent:)])
  29. {
  30. [_touchDelegate tableView:self touchesCancelled:touches withEvent:event];
  31. }
  32. }
  33. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  34. {
  35. [super touchesEnded:touches withEvent:event];
  36. if ([_touchDelegate conformsToProtocol:@protocol(JXTableViewDelegate)] &&
  37. [_touchDelegate respondsToSelector:@selector(tableView:touchesEnded:withEvent:)] )
  38. {
  39. [_touchDelegate tableView:self touchesEnded:touches withEvent:event];
  40. }
  41. }
  42. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  43. {
  44. [super touchesMoved:touches withEvent:event];
  45. if ([_touchDelegate conformsToProtocol:@protocol(JXTableViewDelegate)] &&
  46. [_touchDelegate respondsToSelector:@selector(tableView:touchesMoved:withEvent:)])
  47. {
  48. [_touchDelegate tableView:self touchesMoved:touches withEvent:event];
  49. }
  50. }
  51. - (void) gotoFistArrayRow:(int)nnn{
  52. if (nnn>10) {
  53. }else{
  54. [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
  55. }
  56. }
  57. - (void) gotoLastRow:(BOOL)animated{
  58. NSInteger n = [self numberOfRowsInSection:0]-1;
  59. if(n>=1){
  60. [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:n inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:animated];}
  61. }
  62. - (void) gotoFirstRow:(BOOL)animated{
  63. NSInteger n = [self numberOfRowsInSection:0]-1;
  64. if(n>=1)
  65. [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:animated];
  66. }
  67. -(void)gotoRow:(int)n{
  68. if(n<0)
  69. return;
  70. if([self numberOfRowsInSection:0] > n)
  71. [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:n inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
  72. }
  73. - (void)showEmptyImage:(EmptyType)emptyType{
  74. return;
  75. if(!_empty){
  76. _empty = [[UIImageView alloc]initWithFrame:CGRectMake((JX_SCREEN_WIDTH-56)/2, self.frame.size.height/5, 56, 108)];
  77. _empty.image = [UIImage imageNamed:@"no_data_for_the_time_being"];
  78. // _empty.backgroundColor = [UIColor magentaColor];
  79. [self addSubview:_empty];
  80. }
  81. if (!_tipLabel) {
  82. _tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(_empty.frame.origin.x, _empty.frame.origin.y + _empty.frame.size.height + 10, JX_SCREEN_WIDTH, 30)];
  83. CGPoint centerPoint = CGPointMake(_empty.center.x, _empty.frame.origin.y + _empty.frame.size.height + 10);
  84. _tipLabel.center = centerPoint;
  85. _tipLabel.font = g_factory.font16;
  86. _tipLabel.textAlignment = NSTextAlignmentCenter;
  87. // _tipLabel.backgroundColor = [UIColor cyanColor];
  88. [self addSubview:_tipLabel];
  89. }
  90. switch (emptyType) {
  91. case EmptyTypeNoData:
  92. _tipLabel.text = Localized(@"JX_NoData");
  93. break;
  94. case EmptyTypeNetWorkError:
  95. _tipLabel.text = Localized(@"JX_NetWorkError");
  96. default:
  97. break;
  98. }
  99. if (!_tipBtn) {
  100. _tipBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, _tipLabel.frame.origin.y + _tipLabel.frame.size.height + 20, 120, 40)];
  101. [_tipBtn setTitle:Localized(@"JX_LoadAgain") forState:UIControlStateNormal];
  102. [_tipBtn setBackgroundColor:THEMECOLOR];
  103. _tipBtn.layer.masksToBounds = YES;
  104. _tipBtn.layer.cornerRadius = 7.f;
  105. _tipBtn.center = CGPointMake(JX_SCREEN_WIDTH/2, _tipBtn.center.y);
  106. [_tipBtn addTarget:self.delegate
  107. action:@selector(getServerData) forControlEvents:UIControlEventTouchUpInside];
  108. [self addSubview:_tipBtn];
  109. }
  110. }
  111. -(void)hideEmptyImage{
  112. if (_empty) {
  113. [_empty removeFromSuperview];
  114. _empty = nil;
  115. }
  116. if (_tipLabel) {
  117. [_tipLabel removeFromSuperview];
  118. _tipLabel = nil;
  119. }
  120. if (_tipBtn) {
  121. [_tipBtn removeFromSuperview];
  122. _tipBtn = nil;
  123. }
  124. }
  125. -(void)onAfterLoad{
  126. if (self.numberOfSections > 0) {
  127. if([self numberOfRowsInSection:0]<=0){
  128. //[self showEmptyImage:nil];
  129. }else{
  130. [self hideEmptyImage];
  131. }
  132. }
  133. }
  134. -(void)reloadData{
  135. [self clearPool];
  136. [super reloadData];
  137. [self onAfterLoad];
  138. }
  139. -(void)addToPool:(id)p{
  140. if([_pool indexOfObject:p] == NSNotFound){
  141. [_pool addObject:p];
  142. p = nil;
  143. }
  144. }
  145. -(void)delFromPool:(id)p{
  146. [_pool removeObject:p];
  147. }
  148. -(void)clearPool{
  149. for (NSInteger i=[_pool count]-1; i>0;i--){
  150. UITableViewCell* p = [_pool objectAtIndex:i];
  151. [_pool removeObjectAtIndex:i];
  152. // [p removeFromSuperview];
  153. p = nil;
  154. }
  155. }
  156. - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation{
  157. [self hideEmptyImage];
  158. [super insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
  159. }
  160. -(void)reloadRow:(int)n section:(int)section{
  161. NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
  162. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:n inSection:section];
  163. [indexPaths addObject:indexPath];
  164. [self beginUpdates];
  165. [self reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
  166. [self endUpdates];
  167. // [self reloadData];
  168. }
  169. -(void)insertRow:(int)n section:(int)section{
  170. NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
  171. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:n inSection:section];
  172. [indexPaths addObject:indexPath];
  173. @try {
  174. [self beginUpdates];
  175. [self insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
  176. [self endUpdates];
  177. } @catch (NSException *exception) {
  178. } @finally {
  179. }
  180. // [self reloadData];
  181. }
  182. -(void)deleteRow:(int)n section:(int)section{
  183. NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
  184. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:n inSection:section];
  185. [indexPaths addObject:indexPath];
  186. [self beginUpdates];
  187. [self deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
  188. [self endUpdates];
  189. // [self reloadData];
  190. }
  191. @end