SocialTableViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // SocialTableViewController.m
  3. // NIMDemo
  4. //
  5. // Created by Fenix Wang on 2017/6/20.
  6. // Copyright © 2017年 Netease. All rights reserved.
  7. //
  8. #import "SocialTableViewController.h"
  9. #import "SocialPublishViewController.h"
  10. #import "SocialTableViewCell.h"
  11. #import "HttpRequest.h"
  12. #import "UIView+NTES.h"
  13. @interface SocialTableViewController () <UIScrollViewDelegate>
  14. @property (nonatomic, strong) NSMutableArray *dataList;
  15. @property (nonatomic, assign) BOOL isFetching;
  16. @property (nonatomic, assign) BOOL flagRefresh;
  17. @end
  18. @implementation SocialTableViewController
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. // Uncomment the following line to preserve selection between presentations.
  22. // self.clearsSelectionOnViewWillAppear = NO;
  23. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  24. // self.navigationItem.rightBarButtonItem = self.editButtonItem;
  25. _flagRefresh = NO;
  26. [self fetchDataList];
  27. }
  28. - (void)didReceiveMemoryWarning {
  29. [super didReceiveMemoryWarning];
  30. // Dispose of any resources that can be recreated.
  31. }
  32. - (void)viewWillAppear:(BOOL)animated{
  33. [super viewWillAppear:animated];
  34. UINavigationItem *navItem = self.parentViewController.navigationItem;
  35. navItem.title = @"朋友圈";
  36. UIButton *emptyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  37. [emptyBtn setTitle:@"" forState:UIControlStateNormal];
  38. [emptyBtn sizeToFit];
  39. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:emptyBtn];
  40. [navItem setLeftBarButtonItems:@[leftItem]];
  41. UIButton *publishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  42. [publishBtn setTitle:@"发布" forState:UIControlStateNormal];
  43. [publishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  44. [publishBtn addTarget:self action:@selector(onTouchPublish:) forControlEvents:UIControlEventTouchUpInside];
  45. [publishBtn sizeToFit];
  46. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:publishBtn];
  47. [navItem setRightBarButtonItems:@[rightItem]];
  48. }
  49. - (void)fetchDataList{
  50. if(_isFetching)
  51. return;
  52. _isFetching = YES;
  53. NSString *lastId = nil;
  54. if(self.dataList && self.dataList.count){
  55. SocialItemData *lastData = [self.dataList lastObject];
  56. lastId = lastData.socialId;
  57. }
  58. [[HttpRequest shared] getSocialDataList:lastId success:^(NSMutableArray *dataList) {
  59. if(!self.dataList)
  60. self.dataList = dataList;
  61. else
  62. [self.dataList addObjectsFromArray:dataList];
  63. [self.tableView reloadData];
  64. _isFetching = NO;
  65. } failure:^{
  66. _isFetching = NO;
  67. }];
  68. }
  69. #pragma mark - Table view data source
  70. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  71. #warning Incomplete implementation, return the number of sections
  72. return 1;
  73. }
  74. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  75. #warning Incomplete implementation, return the number of rows
  76. if(_dataList)
  77. return _dataList.count;
  78. return 0;
  79. }
  80. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  81. NSString *cellId = @"social";
  82. SocialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
  83. if(!cell)
  84. {
  85. cell = [[SocialTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
  86. [cell initWidth:self.view.frame.size.width];
  87. }
  88. SocialItemData *data = [_dataList objectAtIndex:indexPath.row];
  89. [cell setData:data];
  90. return cell;
  91. }
  92. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  93. SocialItemData *data = [self.dataList objectAtIndex:indexPath.row];
  94. return data.cellHeight;
  95. }
  96. /*
  97. // Override to support conditional editing of the table view.
  98. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  99. // Return NO if you do not want the specified item to be editable.
  100. return YES;
  101. }
  102. */
  103. /*
  104. // Override to support editing the table view.
  105. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  106. if (editingStyle == UITableViewCellEditingStyleDelete) {
  107. // Delete the row from the data source
  108. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  109. } else if (editingStyle == UITableViewCellEditingStyleInsert) {
  110. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  111. }
  112. }
  113. */
  114. /*
  115. // Override to support rearranging the table view.
  116. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
  117. }
  118. */
  119. /*
  120. // Override to support conditional rearranging of the table view.
  121. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
  122. // Return NO if you do not want the item to be re-orderable.
  123. return YES;
  124. }
  125. */
  126. /*
  127. #pragma mark - Navigation
  128. // In a storyboard-based application, you will often want to do a little preparation before navigation
  129. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  130. // Get the new view controller using [segue destinationViewController].
  131. // Pass the selected object to the new view controller.
  132. }
  133. */
  134. - (void)onTouchPublish:(int)sender
  135. {
  136. UIViewController *vc = [[SocialPublishViewController alloc] init];
  137. [self.navigationController pushViewController:vc animated:YES];
  138. }
  139. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  140. //康海涛测试Ok的
  141. CGPoint offset1 = scrollView.contentOffset;
  142. CGRect bounds1 = scrollView.bounds;
  143. CGSize size1 = scrollView.contentSize;
  144. UIEdgeInsets inset1 = scrollView.contentInset;
  145. float y1 = offset1.y + bounds1.size.height - inset1.bottom;
  146. float h1 = size1.height;
  147. //NSLog(@"%f %f %i %f", y1, h1, _flagRefresh, bounds1.size.height);
  148. if(h1 < bounds1.size.height)
  149. return;
  150. if (y1 > h1 + 50) {
  151. _flagRefresh = YES;
  152. }
  153. else if (y1 == h1) {
  154. if(_flagRefresh)
  155. {
  156. _flagRefresh = NO;
  157. NSLog(@"下拉刷新");
  158. [self fetchDataList];
  159. }
  160. }
  161. }
  162. @end