CollectionView加下拉刷新和上拉加载更多

#import "ShopMembersCollectionVC.h"

#import "ContactsSearchVC.h"

#import "ContactCollectionViewCell.h"

#import "RequestClient+Shop.h"

#import "RemoveMemberViewController.h"

#import "InviteNewMemberView.h"

#import "ListDataModule.h"

#import <MJRefresh/MJRefresh.h>

 

@interface ShopMembersCollectionVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) ListDataModule *listData;

@end

 

@implementation ShopMembersCollectionVC

 

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor whiteColor]]];

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];

    [self.navigationController setNavigationBarHidden:NO animated:NO];

    

}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = kColor_White;

    self.navigationItem.title = @"公会成员";

    

    UIView * headerBg =  [UICommonCtrl commonViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) color:kColor_White];

    [self.view addSubview:headerBg];

    

    UIButton * searchBtn =  [UICommonCtrl commonButtonWithFrame:CGRectMake(15, 7, SCREEN_WIDTH - 30, 30)

                                                        image:nil

                                                       target:self

                                                       action:@selector(clickSearchBtn) ];

    searchBtn.backgroundColor = kColor_Background;

    searchBtn.layer.cornerRadius = 15;

    [searchBtn.layer setMasksToBounds:YES];

    [headerBg addSubview:searchBtn];

    

    UIImageView * imgSearch = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"search"]];

    imgSearch.left = 7;

    imgSearch.centerY = 15;

    [searchBtn addSubview:imgSearch];

    

    UILabel *  placeHolder = [UICommonCtrl commonLabelWithFrame:CGRectMake(imgSearch.right + 10, 0, 50, 30)

                                                           text:@"搜索"

                                                          color:kColor_Gray

                                                           font:FONT(13)

                                                  textAlignment:NSTextAlignmentLeft];

    

    [searchBtn addSubview:placeHolder];

    

 

    UIView * line = [UICommonCtrl commonLineViewWithFrame:CGRectMake(0, 50-LINE_SIZE, SCREEN_WIDTH, LINE_SIZE) color:kColor_Line];

    [headerBg addSubview:line];

    

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    //设置水平方向的空隙

    flowLayout.minimumInteritemSpacing = 0;//22;

    //设置垂直方向的空隙

    flowLayout.minimumLineSpacing = 0;//10;

    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

    

    _collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(20.5, headerBg.bottom, SCREEN_WIDTH - 41, SCREEN_HEIGHT_WITHOUTHEAD - headerBg.height) collectionViewLayout:flowLayout];

    _collectionView.delegate = self;

    _collectionView.dataSource = self;

    [_collectionView registerClass:[ContactCollectionViewCell class] forCellWithReuseIdentifier:KCellReuseIdentifier];

    _collectionView.backgroundColor = kColor_White;

    _collectionView.scrollEnabled = YES;

    _collectionView.showsVerticalScrollIndicator= NO;

    

    [self.view addSubview:_collectionView];

    

    //创建自定义下拉刷新

    MJRefreshNormalHeader *header =   [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];

    _collectionView.mj_header = header;

    

    //1.添加翻页数据

    self.listData = [[ListDataModule alloc]init];

 

    [self showLoading];

    [self requestShopMember];

 

}

 

- (void)clickSearchBtn

{

    ContactsSearchVC *vc = [[ContactsSearchVC alloc] init];

    vc.shopIdStr = self.shopIdStr;

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];

    [kAppDelegate.window.rootViewController presentViewController:nav animated:NO completion:nil];

}

 

- (void)refresh

{

    _collectionView.scrollEnabled = NO;

    

    [self.listData resetData];

    [self requestShopMember];

}

- (void)endRefreshing

{

    if(self.collectionView.mj_header){

        [self.collectionView.mj_header endRefreshing];//自定义下拉刷新,需要调用这个关闭刷新状态

    }

    _collectionView.scrollEnabled = YES;

}

 

//LoadMore

- (void)refrshLoadMoreControlWithFull:(BOOL)full

{

    if (self.collectionView.mj_footer == nil) {

        @weakify(self)

        self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{

            @strongify(self)

            [self loadMore];

        }];

    }

    

    if (full) {

        [self.collectionView.mj_footer endRefreshingWithNoMoreData];

    } else {

        [self.collectionView.mj_footer endRefreshing];

    }

}

- (void)loadMore

{

    [self requestShopMember];

}

 

- (void)requestShopMember

{

    

    NSMutableDictionary * param = [NSMutableDictionary dictionary];

 

    [param setObject:self.shopIdStr forKey:@"id"];

   

    self.listData.size = 26;

    [param setObject:STR_NUM(self.listData.size) forKey:@"limit"];

    

    if (self.listData.page == 1) {

          [param setObject:@(0) forKey:@"offset"];

    }else{

        NSDictionary * lastModel = self.listData.data.lastObject;

        NSInteger  lastId = 0;

        if(lastModel && lastModel[@"id"]){

            lastId = [NON(lastModel[@"id"]) integerValue];

        }

        [param setObject:STR_NUM(lastId) forKey:@"offset"];

    }

 

    

    @weakify(self)

    [[RequestClient sharedInstance] requestShopMember:param

                                                   success:^(NSURLSessionDataTask *operation, id responseObject) {

                                                       @strongify(self)

                                                       [self hideLoading];

 

                                                       [self.listData loadData:responseObject listKey:@"members" countKey:@"count"];

                                                       

                                                       if(!self.listData.isEmpty) {//列表不为空才去加载更多

                                                           [self refrshLoadMoreControlWithFull:self.listData.isFull];

                                                       }

                                                       

                                                      [self.collectionView reloadData];

                                                       [self endRefreshing];

 

 

                                                   }

                                                   failure:^(NSURLSessionDataTask *operation, NSError *error) {

                                                       @strongify(self)

                                                       [self showNonet:error];

                                                       [self  endRefreshing];

                                                  

                                                   }];

    

}

 

#pragma mark - UICollectionViewDataSource 、delegate 和 FlowLayoutDelegate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 1;

}

//定义展示的UICollectionViewCell的个数

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    NSInteger add = self.isManager?2:0;

    return self.listData.data.count + add;

}

 

//每个UICollectionView展示的内容

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    if ( indexPath.row == 0 && self.isManager) {

        ContactCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:KCellReuseIdentifier forIndexPath:indexPath];

        cell.headerImageView.image = [UIImage imageNamed:@"increase"];

        cell.name = @"";

 

        return cell;

    }else if( indexPath.row == 1 && self.isManager){

        ContactCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:KCellReuseIdentifier forIndexPath:indexPath];

        cell.headerImageView.image = [UIImage imageNamed:@"Subtract"];

        cell.name = @"";;

 

        return cell;

    }else{

        ContactCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:KCellReuseIdentifier forIndexPath:indexPath];

         NSInteger add = self.isManager?2:0;

        NSDictionary *model = self.listData.data[indexPath.row - add];

        cell.headimg = model[@"avatar"];

        cell.name = model[@"name"];

        return cell;

    }

 

 

}

 

//返回这个UICollectionView是否可以被选择

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

    if ((indexPath.row == 0 && self.isManager)) {

        return YES;

    }else if (indexPath.row == 1 && self.isManager){

        return YES;

    }

    

    return  NO;

}

 

//定义每个UICollectionView 的大小

- (CGSize)collectionView:(UICollectionView *)collectiagreeMsgonView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    return CGSizeMake(kCellW, kCellH);

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

    [collectionView deselectItemAtIndexPath:indexPath animated:YES];

    

    if (indexPath.row == 0 && self.isManager) {

        InviteNewMemberView * newM = [[InviteNewMemberView alloc]init];

        newM.shopIdStr = self.shopIdStr;

        [newM show];

    }else if(indexPath.row == 1 && self.isManager){

        RemoveMemberViewController * vc = [[RemoveMemberViewController alloc]init];

        vc.shopIdStr = self.shopIdStr;

        @weakify(self)

        vc.removeSuccessBlock = ^{

            @strongify(self)

            [self.collectionView.mj_header beginRefreshing];

            if (self.moveSuccessBlock) {

                self.moveSuccessBlock();

            }

        };

        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];

        [self.navigationController presentViewController:nav animated:YES completion:^{

            

        }];

    }

    

}

//无网络时,cell touch 事件

- (void)nonetViewTouched

{

    //加载数据

    [self showLoading];

    [self requestShopMember];

}

 

@end

posted @ 2019-03-07 17:38  湘岳  阅读(1374)  评论(0编辑  收藏  举报