ifish/Ifish/controllers/IfishTabControllers/探索/IfishUsersActivity/ViewControlller/IfishUsersActivityListContr...

230 lines
7.3 KiB
Objective-C

//
// IfishUsersActivityListController.m
// Ifish
//
// Created by imac on 2017/5/11.
// Copyright © 2017年 lianlian. All rights reserved.
//
#import "IfishUsersActivityListController.h"
#import "IFishUserActivityData.h"
#import "IFishUserActivityListCell.h"
#import "FishActivityData.h"
#import "JHRefresh.h"
#import "IfishUserDefaultHelper.h"
const float ActivityPageSize = 20;
@interface IfishUsersActivityListController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic,strong) NSMutableArray *activTypes;
@property (nonatomic) NSInteger firstResult;
@property(nonatomic,copy) NSString* total;
@property(nonatomic)BOOL isLoadMore;
@property(nonatomic) int page;
@end
@implementation IfishUsersActivityListController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self addTitleViewWithTitle:@"实时动态"];
_dataArr =[[NSMutableArray alloc] init];
_page = 0;
[self loadActypes];
[self creatTab];
[self requestListData];
[self creatReatRefreshView];
}
-(void)loadActypes{
_activTypes = [[NSMutableArray alloc] init];
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"ActivityData" ofType:@"json"];
NSData *data = [[NSData alloc]initWithContentsOfFile:filePath];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray*typeListArr = [dic objectForKey:@"data"];;
for (NSDictionary *typedict in typeListArr ) {
FishActivityData *model = [[FishActivityData alloc] initWithDict:typedict];
[_activTypes addObject:model];
}
}
-(void)requestListData{
NSString *useriD= [dataContorl dataControlGetUserIdInfo];
NSInteger pageSize = ActivityPageSize ;
_firstResult = 0;
__weak typeof (self)weakSelf = self;
[AFHttpTool getUserActivity:useriD pageSize:pageSize firstResult:_firstResult success:^(id response) {
NSDictionary *reDic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
if ([reDic[@"result"] isEqualToString:@"100"]){
_total = [reDic objectForKey:@"total"];
NSArray *daA =reDic[@"data"];
for (NSDictionary *dic in daA) {
IFishUserActivityData *data = [[IFishUserActivityData alloc] initWithDict:dic];
[_dataArr addObject:data];
}
//存储当前最大Id
IFishUserActivityData *data = _dataArr[0];
[IfishUserDefaultHelper saveUserActivityMaxId:data.activityId];
[weakSelf.tableView reloadData];
}else{
[weakSelf.view makeToast:@"请求失败"];
}
} failure:^(NSError *err) {
[weakSelf.view makeToast:@"请求异常"];
}];
}
-(void)creatTab{
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStyleGrouped];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.scrollEnabled = YES;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.showsVerticalScrollIndicator = NO;
self.tableView.backgroundColor = RGB(242, 242, 242);
[self.view addSubview:self.tableView];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
IFishUserActivityListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IFishUserActivityListCell"];
if (!cell) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"IFishUserActivityListCell" owner:self options:nil]lastObject];
}
IFishUserActivityData *data = _dataArr[indexPath.section];
[cell loadDataWith:data typeArr:_activTypes];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 124;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section==0) {
return 0.01;
}else{
return 10;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.01;
}
-(void)creatReatRefreshView{
__weak typeof (self)weskSelf=self;
//上拉加载更多
[weskSelf.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
if (weskSelf.isLoadMore) {
return ;
}
weskSelf.isLoadMore=YES;
NSInteger pagecount =[_total integerValue]/ActivityPageSize + 1;
if (_page<pagecount-1) {
_page++;
//firstResult 服务器返回数据知道总数total 获取分页数据 暂不需要
NSInteger first=(NSInteger)_page*ActivityPageSize ;
NSInteger pageSize= ActivityPageSize ;
[weskSelf addTaskWithfirsStr:first pageSize:pageSize isRefresh:NO];
}else{
[weskSelf endRefreshing];
[weskSelf.view makeToast:@"已无更多数据"];
}
}];
}
-(void)endRefreshing{
if (self.isLoadMore) {
self.isLoadMore=NO;
[self.tableView footerEndRefreshing];
}
}
-(void)addTaskWithfirsStr:(NSInteger)firsStr pageSize:(NSInteger)pageSize isRefresh:(BOOL)isRefresh{
__weak typeof (self)weskSelf=self;
NSString *userId= [dataContorl dataControlGetUserIdInfo];
[AFHttpTool getUserActivity:userId pageSize:pageSize firstResult:firsStr success:^(id response) {
NSDictionary *reDic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
// if (isRefresh) {
// [weskSelf.dataArr removeAllObjects];
// }
if ([reDic[@"result"] isEqualToString:@"100"]) {
NSArray *daA =reDic[@"data"];
for (NSDictionary *dic in daA) {
IFishUserActivityData *data = [[IFishUserActivityData alloc] initWithDict:dic];
[_dataArr addObject:data];
}
_total = reDic[@"total"];
[weskSelf.tableView reloadData];
}else{
[weskSelf.view makeToast:@"请求失败"];
}
[weskSelf endRefreshing];
} failure:^(NSError *err) {
[weskSelf endRefreshing];
[weskSelf.view makeToast:@"没有加载成功"];
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end