(OC)iOS开发用AFNetworking和MJRefresh实现网络请求和下拉刷新、上拉加载

  1. #import "ViewController.h"  
  2. #import "AFNetworking.h"  
  3. #import "MJRefresh.h"  
  4. #import "TestTableViewCell.h"  
  5.   
  6. @interface ViewController () <UITableViewDelegate, UITableViewDataSource>  
  7.   
  8. @property (nonatomicstrongUITableView *tableView;  
  9. @property (nonatomicstrongNSMutableArray *dataArray;  
  10. @property (assign, nonatomicint page;  
  11.   
  12. @end  
  13.   
  14. @implementation ViewController  
  15.   
  16. static NSString *identifier = @"cell";  
  17.   
  18. - (void)viewDidLoad {  
  19.     [super viewDidLoad];  
  20.     self.dataArray = [NSMutableArray array];  
  21.       
  22.     self.tableView = [[UITableView alloc] init];  
  23.     self.tableView.frame = CGRectMake(00375667);  
  24.     self.tableView.backgroundColor = [UIColor colorWithRed:0.635 green:1.000 blue:0.905 alpha:1.000];  
  25.     self.tableView.delegate = self;  
  26.     self.tableView.dataSource = self;  
  27.     [self.view addSubview:self.tableView];  
  28.     // cell自适应高度  
  29.     self.tableView.estimatedRowHeight = 100;  
  30.     self.tableView.rowHeight = UITableViewAutomaticDimension;  
  31.       
  32.     [self.tableView registerNib:[UINib nibWithNibName:@"TestTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];  
  33.       
  34.     self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{  
  35.         self.page = 0;  
  36.         [self updateData];  
  37.     }];  
  38.     self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{  
  39.         [self updateData];  
  40.     }];  
  41. }  
  42.   
  43. - (void)viewWillAppear:(BOOL)animated {  
  44.     [super viewWillAppear:animated];  
  45.     [self.tableView.mj_header beginRefreshing];  
  46. }  
  47.   
  48. - (void)updateData {  
  49.     NSString * urlStr = [NSString stringWithFormat:@"http://www.ios122.com/find_php/index.php?viewController=YFPostListViewController&model[category]=ui&model[page]=%d"self.page++];  
  50.     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];  
  51.     [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {  
  52.           
  53.     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {  
  54.         [self.tableView.mj_header endRefreshing];  
  55.         [self.tableView.mj_footer endRefreshing];  
  56.         if (self.page == 1) {  
  57.             // 如果是下拉刷新数据,将所有数据移除,再重新添加刚刷新的数据  
  58.             for (;0 < self.dataArray.count;) {  
  59.                 [self.dataArray removeObjectAtIndex:0];  
  60.             }  
  61.         }  
  62.         // 将刷新到的数据添加到数组的后面  
  63.         for (NSMutableDictionary *item in responseObject) {  
  64.             if (item != (NSMutableDictionary *)[NSNull null]) {  
  65.                 [self.dataArray addObject:item];  
  66.             }  
  67.         }  
  68.         [self.tableView reloadData];  
  69.     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {  
  70.         [self.tableView.mj_header endRefreshing];  
  71.         [self.tableView.mj_footer endRefreshing];  
  72.     }];  
  73. }  
  74.   
  75. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
  76.     return self.dataArray.count;  
  77. }  
  78.   
  79. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  80.     TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];  
  81.     cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  82.     NSDictionary *dict = self.dataArray[indexPath.row];  
  83.     NSString * content = [NSString stringWithFormat:@"标题:%@ 内容:%@ 标题:%@ 内容:%@ 标题:%@ 内容:%@ 标题:%@ 内容:%@ 标题:%@ 内容:%@ 标题:%@ 内容:%@", dict[@"title"], dict[@"desc"], dict[@"title"], dict[@"desc"], dict[@"title"], dict[@"desc"], dict[@"title"], dict[@"desc"], dict[@"title"], dict[@"desc"], dict[@"title"], dict[@"desc"]];  
  84.     cell.label.text = content;  
  85.     cell.label.numberOfLines = 0;  
  86.     cell.backgroundColor = [UIColor colorWithRed:0.543 green:0.854 blue:1.000 alpha:1.000];  
  87.     return cell;  
  88. }  
  89.   
  90. @end  

猜你喜欢

转载自blog.csdn.net/mr_tangit/article/details/80630225