首先感谢iOS122提供的可以免费GET请求到的网络数据的接口
为了方便cell自适应高度,此处的cell是带Xib的。为了方便理解代码,此处没有应用MVC设计模式,实际开发中不能这样。
#import "ViewController.h" #import "AFNetworking.h" #import "MJRefresh.h" #import "TestTableViewCell.h" @interface ViewController () <UITableViewDelegate, UITableViewDataSource> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataArray; @property (assign, nonatomic) int page; @end @implementation ViewController static NSString *identifier = @"cell"; - (void)viewDidLoad { [super viewDidLoad]; self.dataArray = [NSMutableArray array]; self.tableView = [[UITableView alloc] init]; self.tableView.frame = CGRectMake(0, 0, 375, 667); self.tableView.backgroundColor = [UIColor colorWithRed:0.635 green:1.000 blue:0.905 alpha:1.000]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; // cell自适应高度 self.tableView.estimatedRowHeight = 100; self.tableView.rowHeight = UITableViewAutomaticDimension; [self.tableView registerNib:[UINib nibWithNibName:@"TestTableViewCell" bundle:nil] forCellReuseIdentifier:identifier]; self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ self.page = 0; [self updateData]; }]; self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ [self updateData]; }]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.tableView.mj_header beginRefreshing]; } - (void)updateData { NSString * urlStr = [NSString stringWithFormat:@"http://www.ios122.com/find_php/index.php?viewController=YFPostListViewController&model[category]=ui&model[page]=%d", self.page++]; AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { [self.tableView.mj_header endRefreshing]; [self.tableView.mj_footer endRefreshing]; if (self.page == 1) { // 如果是下拉刷新数据,将所有数据移除,再重新添加刚刷新的数据 for (;0 < self.dataArray.count;) { [self.dataArray removeObjectAtIndex:0]; } } // 将刷新到的数据添加到数组的后面 for (NSMutableDictionary *item in responseObject) { if (item != (NSMutableDictionary *)[NSNull null]) { [self.dataArray addObject:item]; } } [self.tableView reloadData]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [self.tableView.mj_header endRefreshing]; [self.tableView.mj_footer endRefreshing]; }]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; NSDictionary *dict = self.dataArray[indexPath.row]; 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"]]; cell.label.text = content; cell.label.numberOfLines = 0; cell.backgroundColor = [UIColor colorWithRed:0.543 green:0.854 blue:1.000 alpha:1.000]; return cell; } @end
时间: 2024-10-14 10:42:28