上拉刷新,下拉加载

使用MJRefresh第三方。

1.网络请求 

#pragma mark -- 网络请求
- (void)requestData
{
    
    NSString * urlString = [NSString stringWithFormat:@"%@%@",SERVER_URL,SearchVideo];
    NSString * pageNo = [NSString stringWithFormat:@"%d",_page];
    NSDictionary *parameter = @{@"rowNum":@"10",@"pageNo":pageNo,@"imei":@"863247020616135",@"keyword":_textField.text};
    NSString * str = [parameter JSONRepresentation] ;
    NSString * newUrlString = [[NSString stringWithFormat:@"%@%@",urlString,str]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [AFRequest PostRequestWithUrl:newUrlString parameters:nil andBlock:^(id Datas, NSError *error) {
        if (error == nil)
        {
             if ([[Datas objectForKey:@"rescode"] isEqualToString:@"0000"])
            {
                NSArray *arr = Datas[@"result"];
                if (arr.count>0) {
                    for (int i = 0; i < arr.count; i++)
                    {
                        SProgramListModel *model = [SProgramListModel objectWithKeyValues:arr[i]];
                        [_dataArray addObject:model];
                    }
                    [self doneWithView:_footer];
                }else{
                    [self showMsg:@"没有查找的视频了哦"];
                    [self doneWithView:_footer];
                }
            }
            
            if (_BrowserPicView == nil)
            {
                [self addCollectionView];
            } else {
                [_BrowserPicView reloadData];
            }
            
            if (_dataArray.count == 0) {
                _errorView.hidden = NO;
            } else {
                _errorView.hidden = YES;
            }
            
            [_BrowserPicView reloadData];
        }
    }];
}

2.添加View

- (void)addCollectionView
{
    CGRect viewFrame = CGRectMake(0, 10, SCREEN_WIDTH, SCREEN_HEIGHT - 64-10);
    
    //添加collectionView
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;//滚动方向
    _BrowserPicView = [[UICollectionView alloc] initWithFrame:viewFrame collectionViewLayout:layout];
    _BrowserPicView.alwaysBounceVertical = YES;
    _BrowserPicView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:_BrowserPicView];
    
    //注册collectionview
    [_BrowserPicView registerClass:[SProgramListCollectionViewCell class]
        forCellWithReuseIdentifier:@"SearchVideo"];//视频列表
    
    _BrowserPicView.delegate = self;
    _BrowserPicView.dataSource = self;
    _BrowserPicView.backgroundColor = c8;
    
    [self addRefresh];
}

3.添加刷新控件

#pragma mark -- 添加下拉刷新
- (void)addRefresh
{
    _header = [MJRefreshHeaderView header];
    _header.scrollView = _BrowserPicView;
    _header.delegate = self;
    
    _footer = [MJRefreshFooterView footer];
    _footer.scrollView = _BrowserPicView;
    _footer.delegate = self;
}


#pragma mark 开始刷新数据
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if (refreshView == _footer){
        _page ++;
    }else {
        _page = 1;
        [_dataArray removeAllObjects];
    }
    [self requestData];
    [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:1.5];
}

#pragma mark 刷新表格并且结束正在刷新状态
- (void)refreshViewEndRefreshing:(MJRefreshBaseView *)refreshView
{
    [refreshView endRefreshing];
}

- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    [refreshView endRefreshing];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值