使用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];
}