IOS UICollectionView 横向分页加载(左滑加载数据)

@interface ONMessageHeaderView ()

@property (strong, nonatomic)UICollectionView collectionView;
@property (nonatomic , assign) NSInteger visitorPage;
/
*

  • 旋转图标
    */
    @property (nonatomic, weak) UIActivityIndicatorView *indicatorView;
    @end

@implementation ONMessageHeaderView

  • (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {

      [self setUpChildView];
      // 获取访客细心记录
      _visitorPage = 1;
      //        [self InitVisitData];
    

    }
    return self;
    }

  • (void)setUpChildView{

    CGFloat startX = 10;

    CGFloat startY = 5;

    CGFloat cgStartY = 13;

    //背景
    UIView * bgV = [[UIView alloc] initWithFrame:CGRectMake(0, cgStartY, SCREEN_WIDTH, self.height - cgStartY * 2)];

    bgV.backgroundColor = [UIColor whiteColor];

    [self addSubview:bgV];

    UIImageView * seeImage = [[UIImageView alloc] initWithFrame:CGRectMake(startX,startY + 5, 25, 20)];

    seeImage.image = [UIImage imageNamed:@"message_Visitor"];

    [bgV addSubview:seeImage];

    //标题
    UILabel * titleL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxY(seeImage.frame) +10, startY, SCREEN_WIDTH, 30)];

    titleL.text = @"我的访客";

    titleL.font = ONFont(16);

    [bgV addSubview:titleL];

    //布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    layout.itemSize = CGSizeMake(ONCellWidth, ONCellHight);

    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

    layout.sectionInset = UIEdgeInsetsMake(0,10, 0, 10);

    layout.minimumInteritemSpacing = 10;

    layout.minimumLineSpacing = 10;

    //访客
    UICollectionView * visiterV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(titleL.frame) +5, SCREEN_WIDTH, ONCellHight) collectionViewLayout:layout];

    visiterV.showsHorizontalScrollIndicator = NO;

    visiterV.backgroundColor = [UIColor whiteColor];

    self.collectionView = visiterV;

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];

    visiterV.collectionViewLayout = layout;

    visiterV.delegate = self;

    visiterV.dataSource = self;

    [bgV addSubview:visiterV];

}

  • (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _visitorRecordArray.count;
    }

  • (UICollectionViewCell * )collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * CellIdentifier = @"UICollectionViewCell";
    VisitorRecordModel *user = _visitorRecordArray[indexPath.row];
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    ONHeadBtn btn = [[ONHeadBtn alloc]initWithFrame:cell.contentView.bounds];
    btn.user = user;
    // UIImageView * iconV = [[UIImageView alloc] initWithFrame:cell.contentView.bounds];
    //
    // iconV.layer.cornerRadius = iconV.width
    0.5;
    //
    // iconV.layer.masksToBounds = YES;
    // iconV.image = [UIImage imageNamed:@"占位"];
    // [iconV sd_setImageWithURL:[NSURL URLWithString:user.headPicAddr]];

    for (id subView in cell.contentView.subviews) {
    [subView removeFromSuperview];
    }
    [cell.contentView addSubview:btn];
    return cell;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
self.visitorblock(indexPath.item);
}

  • (void)setVisitorRecordArray:(NSArray *)visitorRecordArray{
    _visitorRecordArray = visitorRecordArray;
    [_collectionView reloadData];
    }

pragma mark 访客记录数据加载

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if(scrollView == self.collectionView){
    //检测左测滑动,开始加载更多
    if(scrollView.contentOffset.x +scrollView.width - scrollView.contentSize.width >30){
    NSLog(@"scrollview.contentOffset.x-->%f,scrollview.width-->%f,scrollview.contentsize.width--%f",scrollView.contentOffset.x,scrollView.width,scrollView.contentSize.width);

          if (self.indicatorView == nil) {
              UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(scrollView.width - 20, scrollView.y + scrollView.height/2 - 10, 20, 20)];
              indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
              indicatorView.hidesWhenStopped = YES;
              self.indicatorView = indicatorView;
              [self.indicatorView stopAnimating];
              [scrollView.superview addSubview:self.indicatorView];
    
          }
          if (!self.indicatorView.isAnimating) {
              scrollView.x = -30;
    
              [self.indicatorView startAnimating];
              [self loadMoreTopic];
          }
    
      }
    

    }
    }

/**

  • 加载更多专题
    */
  • (void)loadMoreTopic {

    self.visitorPage ++;
    NSDictionary*parameters=@{@"uuid":[CurrentUserModel sharedCurrentUserModel].uuid,@"page":@(self.visitorPage),@"pageSize":@(10)};
    [ONHttpTool POST2:queryVisitorRecordList parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( 0.5* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
          self.collectionView.x = 0;
          [self.indicatorView stopAnimating];
      });
    
      NSInteger result = [responseObject[@"result"] integerValue];
      if (result == SUCCESS) {
          NSArray *visitorArray = responseObject[@"visitorRecordArray"];
          NSMutableArray *arrar = [NSMutableArray arrayWithArray:self.visitorRecordArray];
          [visitorArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
              VisitorRecordModel *userModel = [VisitorRecordModel userInfoWithDictionary:obj];
              [arrar addObject:userModel];
    
          }];
          self.visitorRecordArray = arrar;
          [self.collectionView reloadData];
    
      }
    

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

      self.collectionView.x = 0;
      [self.indicatorView stopAnimating];
      [YWProgressHUD showError:@"系统繁忙"];
    

    }];

}

@end

你可能感兴趣的:(IOS UICollectionView 横向分页加载(左滑加载数据))