- #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
(OC)iOS开发用AFNetworking和MJRefresh实现网络请求和下拉刷新、上拉加载
猜你喜欢
转载自blog.csdn.net/mr_tangit/article/details/80630225
今日推荐
周排行