问题
I am trying to make an application that uses SBJSON and ASIHTTPRequest containing 273 pages (50 records in each page)in json. When I scroll the tableView I want to get next 50 records when I reach the last record.Currently I am getting only the first 50 records. How will I get all the records? I am using Xcode 6 objective c.
//Here I am taking the json data on view load
- (void)viewDidLoad
{
[super viewDidLoad];
main_array = [[NSMutableArray alloc] init];
NSURL *countryURL = [NSURL URLWithString:@"http://api.worldbank.org/countries/all/indicators/SP.POP.TOTL?format=json"];
ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:countryURL];
[request setDelegate:self];
[request startSynchronous];
[self setTitle:@"COUNTRIES"];
}
-(void) requestFinished: (ASIHTTPRequest *) request
{
NSString *theJSON = [request responseString];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSMutableArray *jsonDictionary = [parser objectWithString:theJSON error:nil];
//Here I am separating the different arrays and storing it in model class
NSDictionary *pagesDict = [jsonDictionary objectAtIndex:0];
NSMutableArray *dataArray = [jsonDictionary objectAtIndex:1];
for (NSDictionary *dictionary in dataArray)
{
Model *model = [[Model alloc]init];
NSDictionary *tmpdict =[dictionary objectForKey:@"indicator"];
NSDictionary *tmpcountdict =[dictionary objectForKey:@"country"];
model.indicator = [tmpdict objectForKey:@"value"];
model.country = [tmpcountdict objectForKey:@"value"];
model.date = [dictionary objectForKey:@"date"];
model.value = [dictionary objectForKey:@"value"];
model.decimal = [dictionary objectForKey:@"decimal"];
//main_array is the main mutable array to store values of model class
[main_array addObject:model];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [main_array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SimpleTableViewCell";
SimpleTableViewCell *cell = (SimpleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
Model *model = [[Model alloc]init];
model = [self.main_array objectAtIndex:indexPath.row];
//Display value in cells
cell.indicatorLabel.text = [NSString stringWithFormat:@"%@",model.indicator];
cell.countryLabel.text = [NSString stringWithFormat:@"%@",model.country];
cell.yearLabel.text = [NSString stringWithFormat:@"%@",model.date];
cell.valueLabel.text = [NSString stringWithFormat:@"%@",model.value];
cell.decimalLabel.text = [NSString stringWithFormat:@"%@",model.decimal];
return cell;
}
回答1:
Have you tried using ‘page’ parameter in your query? http://api.worldbank.org/countries/all/indicators/SP.POP.TOTL?format=json&page=2
If you want to list all records, you can use ‘per_page’ parameter, which I would not recommend http://api.worldbank.org/countries/all/indicators/SP.POP.TOTL?format=json&per_page=13640
回答2:
You can set some tag on your last cell and then send call to the server once you reach to that cell. Once you received data from server append it to your table view data and reload the data.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
if(indexPath.row == [data count])
{
cell.tag = SOME_TAG_OF_LAST_CELL;
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(cell.tag == SOME_TAG_OF_LAST_CELL)
{
// send call to server to fetch next page from server.
}
}
来源:https://stackoverflow.com/questions/28851402/display-records-of-next-page-in-json-in-uitableview-on-scrolling-the-tableview-w