//
// WordViewController.m
#import "WordViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "Topic.h"
#import <MJExtension.h>
#import <MJRefresh.h>
@interface WordViewController ()
/**
* 帖子数据
*/
@property(nonatomic,strong) NSMutableArray *topics;
/**
* 当前页码
*/
@property(nonatomic,assign)NSInteger page;
/**
* 当加载下一页数据需要这个参数
*/
@property(nonatomic,copy)NSString *maxtime;
/**
* 上一次请求参数
*/
@property(nonatomic,strong)NSDictionary *params;
@end
@implementation WordViewController
- (NSMutableArray *)topics
{
if (_topics == nil) {
_topics = [NSMutableArray array];
}
return _topics;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 添加刷新控件
[self setupRefresh];
}
/**
* 添加刷新控件
*/
- (void)setupRefresh
{
// 添加头部刷新控件
self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewTopics)];
// 根据拖拽比例自动切换透明度
self.tableView.mj_header.automaticallyChangeAlpha = YES;
// 开始刷新
[self.tableView.mj_header beginRefreshing];
// 添加底部刷新控件
self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopics)];
}
#pragma mark - 数据处理
/**
* 加载新的帖子数据
*/
- (void)loadNewTopics
{
// 结束上拉刷新
[self.tableView.mj_footer endRefreshing];
// 发送网络请求
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"a"] = @"list";
params[@"c"] = @"data";
params[@"type"] = @"29";
// 保存请求参数
self.params = params;
[[AFHTTPSessionManager manager] GET:@"https://api.budejie.com/api/api_open.php" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
// 判断请求参数是否相同
if (self.params != params) return;
// 保存返回的数据
// 存储maxtime
self.maxtime = responseObject[@"info"][@"maxtime"];
// 字典 -> 模型
self.topics = [Topic mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
// 刷新表格
[self.tableView reloadData];
// 结束刷新
[self.tableView.mj_header endRefreshing];
// 清空页码
self.page = 0;
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 判断请求参数是否相同
if (self.params != params) return;
// 结束刷新
[self.tableView.mj_header endRefreshing];
}];
}
/**
* 加载更多帖子数据
*/
- (void)loadMoreTopics
{
// 结束下拉刷新
[self.tableView.mj_header endRefreshing];
// 发送网络请求
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"a"] = @"list";
params[@"c"] = @"data";
params[@"type"] = @"29";
NSUInteger page = self.page + 1;
params[@"page"] = @(page);
params[@"maxtime"] = self.maxtime;
// 保存请求参数
self.params = params;
[[AFHTTPSessionManager manager] GET:@"https://api.budejie.com/api/api_open.php" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
// 判断请求参数是否相同
if (self.params != params) return;
// 保存返回的数据
// 字典 -> 模型
NSArray *newTopic = [Topic mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
[self.topics addObjectsFromArray:newTopic];
// 刷新表格
[self.tableView reloadData];
// 结束刷新
[self.tableView.mj_header endRefreshing];
// 设置页码
self.page = page;
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 判断请求参数是否相同
if (self.params != params) return;
// 结束刷新
[self.tableView.mj_header endRefreshing];
// 恢复页码
self.page--;
}];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
self.tableView.mj_footer.hidden = (self.topics.count == 0);
return self.topics.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
Topic *topic = self.topics[indexPath.row];
cell.textLabel.text = topic.name;
cell.detailTextLabel.text = topic.text;
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:topic.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
return cell;
}
@end
使用MJRefresh给表格添加上拉刷新,下拉刷新
最新推荐文章于 2022-11-13 17:11:44 发布