使用 iOS 中的 NSURL 连接获取分页 URL 数据

Fetch Pagination URL Data with NSURLConnection in iOS

我是 iOS 的新手,制作了一个包含 JSON 解析数据的应用程序,我使用 NSURLConnection 并获取数据,但这里我的 URL 包含分页,所以,我想在 tableView 滚动时获取分页数据,为此我编写了类似

的代码
- (void)viewDidLoad
{
page=0;
NSString *path = [NSString stringWithFormat:@"http://isp.ebizzprojects.com/webservices/testimonial.php?page=%d",page];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:path]];
[NSURLConnection connectionWithRequest:request delegate:self];
}

并且当我的 table 视图像

一样滚动时,我也会发送第二个请求
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
 {
NSInteger currentOffset = scrollView.contentOffset.y;
NSInteger maximumOffset = scrollView.contentSize.height- scrollView.frame.size.height;
if (maximumOffset - currentOffset <= 0)
{
    page = page + 1;
    [self getData];
}
}
 -(void)getData
 {
NSString *path = [NSString stringWithFormat:@"http://isp.ebizzprojects.com/webservices/testimonial.php?page=%d",page];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:path]];
[NSURLConnection connectionWithRequest:request delegate:self];
 }

和NSURL连接委托方法一样

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 {
[_parsedData setLength:0];
 }
 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_parsedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
_parsedData = nil;
_parsedData = [[NSMutableData alloc]init];
[[[UIAlertView alloc]initWithTitle:@"Alert!" message:@"Connection Error" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil]show];
 }
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
 {
NSString *responseString =[[NSString alloc]initWithData:_parsedData encoding:NSUTF8StringEncoding];
NSDictionary *loDict = [responseString JSONValue];
self.responceArray=[loDict valueForKey:@"description"];
self.nameArray=[loDict valueForKey:@"name"];
self.testimonialTable.hidden=FALSE;
[self.testimonialTable reloadData];
}

从我的代码开始,当我滚动 table 视图时,我只得到第二页数据 我想在我的 table 视图滚动时将这两个数据保存到我的 table 视图中给我解决方案。 谢谢。

您需要向数组添加新项目,例如,为 self.responceArrayself.nameArray

创建 NSMutableArray
[self.responceArray addObjectsFromArray:[loDict valueForKey:@"description"]];
[self.nameArray addObjectsFromArray:[loDict valueForKey:@"name"]];