如何在滚动时重新加载表中的数据?在使用API滚动时在tableview中重新加载数据
如果我有一个url“http:....... & start = 0 & limit = 50”。
如何使用递增与50
开始和限值任何人都可以找到一个解决方案在重新加载表中的数据?
如何在滚动时重新加载表中的数据?在使用API滚动时在tableview中重新加载数据
如果我有一个url“http:....... & start = 0 & limit = 50”。
如何使用递增与50
开始和限值任何人都可以找到一个解决方案在重新加载表中的数据?
您需要在this.In为了做到这一点的表视图来实现负载更多的功能,你需要跟踪表视图的索引,而它一旦滚动表到达最后一个单元格,你可以调用api,增加页码,并在获得api的积极响应后将新记录添加到数组中。
请参阅下面的代码以获得更多的理解。
变量初始化
在viewDidLoad方法
arrTableData = [[NSMutableArray alloc]init];
currentPage=-1;
hasNextPage = YES;
在的tableView numberOfRowsInSection方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count = (int)arrTableData.count;
if (hasNextPage)
{
count++;
}
return count;
}
在的cellForRowAtIndexPath方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
if (indexPath.row<arrTableData.count)
{
//Load TableviewCell you intend to show
}
else
{
//Show loading cell
cell = [self loadingCell:indexPath];
if (!isLoadingNextPage)
{
[self fetchData];
}
}
return cell;
}
加载单元代码
-(UITableViewCell *) loadingCell :(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:nil];
cell.backgroundColor = [UIColor clearColor];
UIActivityIndicatorView *activityIndicatorView =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicatorView.color = [UIColor blackColor];
activityIndicatorView.center = CGPointMake(self.tblView.center.x, cell.center.y);
[cell.contentView addSubview:activityIndicatorView];
[activityIndicatorView startAnimating];
cell.userInteractionEnabled = NO;
return cell;
}
API调用执行
-(void)fetchData
{
isLoadingNextPage = YES;
if (currentPage==-1)
currentPage=0;
//API Call
{
DLog(@"API Call Response = %@",response);
isLoadingNextPage = NO;
if (response == nil)
{
hasNextPage=NO;
return;
}
if (success)
{
if (hasNextPage)
{
currentPage++;
}
[arrTableData addObjectsFromArray:response];
}
else
{
hasNextPage=NO;
}
//Reload tableview
}];
}
这个替代的解决方案是 使用SVPullToRefresh库中整合它的无限滚动。 https://github.com/samvermette/SVPullToRefresh
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if(self.comptabl.contentOffset.y >= (self.comptabl.contentSize.height - self.comptabl.bounds.size.height))
{
if(isPageRefreshing==NO){
isPageRefreshing=YES;
[appDelegate showIndicator:nil view1:self.view];
start=start+50;
[self makeRequest:start];
[self.comptabl reloadData];
NSLog(@"called %d",start);
}
}
}
我使用这种方法,但在所有的时间值增加50 ..how我可以限量值
我希望你要整合分页! – kb920
如何添加分页?我没有使用这种方法。 – Justin
检查此博客http://uiroshan.github.io/2015/02/01/ios-uitableview-load-data-while-scrolling/ – kb920