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

首先感谢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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值