// // IfishLiveListViewController.m // CollectionViewTest // // Created by imac on 17/3/27. // Copyright © 2017年 xiang. All rights reserved. // #import "IfishLiveListViewController.h" #import "IfishLiveListViewCell.h" #import "ZWCollectionViewFlowLayout.h" #import "KankanListCollectionViewCell.h" #import "JHRefresh.h" #import "IfishP2PLivePlayViewController.h" #import "ifishLiveListAdViewCell.h" @interface IfishLiveListViewController () { ZWCollectionViewFlowLayout *_flowLayout;//自定义layout BOOL _isRefreshing; BOOL _isLoadMore; } @property (nonatomic,strong) NSMutableArray *bannerDataArr; @property(nonatomic)BOOL isRefreshing; @property(nonatomic)BOOL isLoadMore; @property(nonatomic,strong) NSIndexPath *selectIndexPath; @end @implementation IfishLiveListViewController static NSString * const reuseAdViewIdentifier = @"ifishLiveListAdViewCell"; static NSString * const cellIdentifier = @"KankanListCollectionViewCell"; const float listPageSize = 10.0f; - (instancetype)init { //初始化自定义layout //_flowLayout = [[ZWCollectionViewFlowLayout alloc] init]; //_flowLayout.colMagrin = 5; //_flowLayout.rowMagrin = 5; //_flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5); //_flowLayout.colCount = 2; //_flowLayout.degelate = self; //self = [self initWithCollectionViewLayout:_flowLayout]; UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init]; flow.minimumLineSpacing = 1; flow.minimumInteritemSpacing = .1; CGFloat width = (kScreenSize.width-15*2*KWidth_Scale -10)/2; CGFloat itemH = width *0.61; flow.itemSize = CGSizeMake(width,itemH); self = [self initWithCollectionViewLayout:flow]; if (self) { // insert code here... } return self; } - (void)viewDidLoad{ [super viewDidLoad]; _dataArray = [[NSMutableArray alloc] init]; _bannerDataArr = [[NSMutableArray alloc] init]; _currentPage =0.0; self.collectionView.backgroundColor = RGB(242, 242, 242); //注册显示cell的类型 [self.collectionView registerClass:[ifishLiveListAdViewCell class] forCellWithReuseIdentifier:reuseAdViewIdentifier]; [self.collectionView registerClass:[KankanListCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier]; UINib *cellNib=[UINib nibWithNibName:@"KankanListCollectionViewCell" bundle:nil]; [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:cellIdentifier]; UINib *adcellNib=[UINib nibWithNibName:@"ifishLiveListAdViewCell" bundle:nil]; [self.collectionView registerNib:adcellNib forCellWithReuseIdentifier:reuseAdViewIdentifier]; [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"reusablePlaceHoldfooterView"]; // Do any additional setup after loading the view. [self creatReatRefreshView]; self.userId = [dataContorl dataControlGetUserIdInfo]; [self loadNewBannerData]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(liveRoomDataDidChange:) name:@"liveRoomDataDidChange" object:nil]; } -(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self name:@"liveRoomDataDidChange" object:nil]; } #pragma mark - 刷新 加载时更多 -(void)creatReatRefreshView{ __weak typeof (self)weskSelf=self; // 下拉刷新 [weskSelf.collectionView addRefreshHeaderViewWithAniViewClass:[JHRefreshAmazingAniView class] beginRefresh:^{ if (weskSelf.isRefreshing) { return ; } weskSelf.isRefreshing=YES; _currentPage = 0; NSInteger firstResult = 0; NSInteger pageSize = listPageSize ; [weskSelf initDataWithFirst:firstResult pageSize:pageSize isRefresh:YES]; }]; //上拉加载更多 [weskSelf.collectionView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{ if (weskSelf.isLoadMore) { return ; } weskSelf.isLoadMore=YES; int pagecount =[self.total floatValue]/listPageSize + 1; NSLog(@"page:::::**** %d",pagecount); if (_currentPage>=pagecount) { [weskSelf.view makeToast:@"已经全部加载完毕"]; [weskSelf endRefreshing]; } }]; } #pragma mark -刷新 -(void)initDataWithFirst:(NSInteger)firstResult pageSize:(NSInteger)pageSize isRefresh:(BOOL)isRefres{} -(void)endRefreshing{ if (self.isRefreshing) { self.isRefreshing=NO; [self.collectionView headerEndRefreshingWithResult:JHRefreshResultNone]; } if (self.isLoadMore) { self.isLoadMore=NO; [self.collectionView footerEndRefreshing]; } } #pragma mark -v4.1 后看看banner -(void)loadNewBannerData{ __weak typeof (self)weskSelf=self; [AFHttpTool getIfishBannerData:IfishAdTypeKankan success:^(id response) { NSDictionary *reDic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil]; if ([reDic[@"result"] isEqualToString:@"100"]){ NSArray*bannerArr=reDic[@"data"]; for (NSDictionary *bannerDic in bannerArr) { IfishBannerData *banner=[[IfishBannerData alloc] initWithDict:bannerDic]; [_bannerDataArr addObject:banner]; } NSIndexSet *set = [NSIndexSet indexSetWithIndex:0]; [weskSelf.collectionView reloadSections:set]; } } failure:^(NSError *err) { }]; } - (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. } */ - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; NSLog(@"------------------------------%@ viewWillAppear",[self class]); NSLog(@"type%lu",(unsigned long)self.listType); } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSLog(@"------------------------------%@ viewDidAppear",[self class]); } #pragma mark - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (section==0) { return 1; } return self.dataArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section ==0) { ifishLiveListAdViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseAdViewIdentifier forIndexPath:indexPath]; cell.backgroundColor = [UIColor redColor]; if (_bannerDataArr.count !=0) { NSArray *ads =self.bannerDataArr; NSMutableArray *imgs = [[NSMutableArray alloc] init]; [ads enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isKindOfClass:[IfishBannerData class]]) { IfishBannerData *banner = (IfishBannerData *)obj; [imgs addObject:banner.adImage]; } }]; cell.adView.adList = imgs; __weak typeof (self)weakSelf = self; cell.adView.tapActionBlock =^(LXAdScrollView *adScrollView) { IfishBannerData *bdata = _bannerDataArr[adScrollView.currentPage]; [weakSelf tapAdWithLink:bdata.adLink]; }; }else{ //默认加载本地 cell.adView.adList = @[@"kankan_listbanner.jpg"]; cell.adView.tapActionBlock =^(LXAdScrollView *adScrollView) { }; } return cell; } KankanListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; IfishKankanListModel*model = _dataArray[indexPath.row]; [cell loadDataAt:indexPath withListModel:model]; return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section==0) { return CGSizeMake(self.view.frame.size.width, self.view.frame.size.width*0.427); }else{ CGFloat width = (self.view.frame.size.width-15*2*KWidth_Scale - 10)/2; CGFloat itemH = width *0.61; CGSize itemSize = CGSizeMake(width,itemH); return itemSize; } } //距离上下左右的间距 -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { if (section == 0) { return UIEdgeInsetsMake(0,0,0, 0); } return UIEdgeInsetsMake(5, 15*KWidth_Scale, 5, 15*KWidth_Scale); } -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { //设置最小的列间距 if (section == 1) { return 5.0f; } return 0.0f; } -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { //设置最小的行间距 if (section == 1) { return 10.0f; } return 0.0f; } #pragma mark 加载脚步视图 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 1) { if (kind == UICollectionElementKindSectionFooter){ //空白脚视图防治内容tab栏遮挡 UICollectionReusableView *reusablefooterView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"reusablePlaceHoldfooterView" forIndexPath:indexPath]; return reusablefooterView; }else{ return nil; } }else{ return nil; } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{ if (section==1) { return CGSizeMake(kScreenSize.width, 60); } return CGSizeZero; } #pragma mark /* // Uncomment this method to specify if the specified item should be highlighted during tracking - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath { return YES; } */ /* // Uncomment this method to specify if the specified item should be selected - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath { return YES; } */ /* // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item - (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath { return NO; } - (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { return NO; } - (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { } */ #pragma mark UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { self.selectIndexPath = indexPath; IfishKankanListModel *listModel=self.dataArray[indexPath.row]; [[NSNotificationCenter defaultCenter] postNotificationName:@"childlistViewDidselect" object:listModel]; } #pragma mark - banner 点击事件 -(void)tapAdWithLink:(NSString *)pushlink { if (![pushlink isKindOfClass:[NSNull class]] && pushlink &&![pushlink isEqualToString:@""]) { [[NSNotificationCenter defaultCenter] postNotificationName:@"childlistViewDidselectadView" object:pushlink]; } } #pragma mark UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { //[[NSNotificationCenter defaultCenter] postNotificationName:@"childlistViewDidScroll" object:scrollView]; [self thredshold:0.7f intemPerPage:10 currentPage:_currentPage scrollViewDidScroll:scrollView]; } //当前页面已经划过了 70% 的时候,就请求新的资源,加载数据 thredshold 设置为70% 并不是单页 70% //上拉预加载 -(void)thredshold:(CGFloat)thredshold intemPerPage:(CGFloat)pageSize currentPage:(CGFloat)currentPage scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat current = scrollView.contentOffset.y + scrollView.frame.size.height; CGFloat total = scrollView.contentSize.height; CGFloat ratio = current / total; CGFloat needRead = pageSize * thredshold + currentPage * pageSize; CGFloat totalItem = pageSize * (currentPage + 1); CGFloat newThreshold = needRead / totalItem; if (ratio >= newThreshold){ //request moredata // _currentPage 初始值0 注意最后一页划过%70 后_currentPage 值突变当. 。 int pagecount =[self.total floatValue]/listPageSize + 1; if (_currentPage