NSInteger _currentPageNo;
UIRefreshControl *_refreshControl;
BOOL _isPullRefresh;///判断是否是下拉
BOOL scrolling;///是否正在上拉刷新
_refreshControl = [[UIRefreshControlalloc] initWithFrame:CGRectMake(0.0f, 0.0f, SCREENBOUND.size.width, 50.0f)];
_refreshControl.tintColor = [UIColorcolorWithHexString:@"#AAAAAA"alpha:1.0f];
[_refreshControladdTarget:selfaction:@selector(requestNewData) forControlEvents:UIControlEventValueChanged];
self.refreshControl = _refreshControl;
///添加表格底部的Actitivy;
self.activity= [[UIActivityIndicatorViewalloc]
initWithFrame : CGRectMake(0, 0, 32.0f, 32.0f)] ;
[_activitysetActivityIndicatorViewStyle: UIActivityIndicatorViewStyleGray];
_activity.color=[UIColorblackColor];
[self.tableViewaddSubview:_activity];
viewDidLoad 里面:
///下拉方法。
_currentPageNo = 1;
_isPullRefresh = YES;
[selfrequestData];
#pragma mark ==========下面四个方法是下拉刷新上拉刷新用的==============
- (void)requestData
{
NetworkSuccessBlock successBlock = ^(id data) {
[_activitystopAnimating];
if (_isPullRefresh) {///如果是下拉。
[_questionData removeAllObjects];
}
[_questionData addObjectsFromArray:data[@"questions"]];
[self.tableView reloadData];
scrolling = YES;
[_refreshControlendRefreshing];
};
NetworkFailureBlock failureBlock = ^(NSError *error) {
scrolling = YES;
[_refreshControlendRefreshing];
};
[SJBQuestionModelrequestQuestionList:_questionTypepageNo:[NSStringstringWithFormat:@"%ld", (long)_currentPageNo] pageSize:@"5"isSolved:_isSolvedonSuccess:successBlock onFailure:failureBlock];
}
- (void)requestNewData
{
if (_refreshControl.refreshing) {
scrolling=NO;
_currentPageNo = 1;
_isPullRefresh = YES;
[selfrequestData];
}
}
- (void)requestMoreData
{
_currentPageNo++;
_isPullRefresh = NO;
[selfrequestData];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y + scrollView.frame.size.height > scrollView.contentSize.height && scrolling && scrollView.contentOffset.y > 0.0f) {
_activity.center =CGPointMake(self.tableView.center.x, self.tableView.contentSize.height+kPROffsetY);
CGSize Size =self.tableView.contentSize;
Size.height+=kTableViewPullMoreDataThreadholdHeight;
self.tableView.contentSize=Size;
[_activitystartAnimating];
[selfperformSelector:@selector(requestMoreData) withObject:nilafterDelay:0.0f];
scrolling=NO;
}
}