如何使用json数据在UITableView中实现分页。

时间:2016-06-21 09:00:16

标签: ios objective-c uitableview

  

嗨,大家好,我是jinth数据的popitable uitableivew。我的回复中有多个页面,所以我需要添加uipagination。我已经尝试实现它但是当我检查行数方法的条件时,它给出了错误索引0超出空数组的边界。

  getdetail = [[NSMutableArray alloc]init];
_currentPage = 1;
totalpage = 5;
//[self fetchdata];

}

-(void)fetchdata
{    
token = [[NSUserDefaults standardUserDefaults]objectForKey:@"Accesstoken"];
NSLog(@"token %@", token);
NSLog(@"Accesstoken %@", token);



NSString *urlString = [NSString stringWithFormat:@"http://qa.networc.in:1336/api/dispatcher/rideHistory/%d", _currentPage];

NSURL *theURL = [NSURL URLWithString:urlString];
NSLog(@"%@", theURL);
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL      cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f];

[theRequest setHTTPMethod:@"GET"];

//Pass some default parameter(like content-type etc.)
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[theRequest setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

[theRequest addValue:token forHTTPHeaderField:@"x-access-token"];

NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];


NSDictionary *dataDictionaryResponse = [NSJSONSerialization JSONObjectWithData:theResponseData options:0 error:&theError];
NSLog(@"url to send request= %@",theURL);
NSLog(@"%@",dataDictionaryResponse);





NSURLSession *session = [NSURLSession sharedSession];

NSURLSessionDataTask *task = [session dataTaskWithRequest:theRequest
                                        completionHandler:
                              ^(NSData *data, NSURLResponse *response, NSError *error) {

                                  NSLog(@"Response:%@ %@\n", response, error);
                                  if(error == nil)

                                  {
                                      // use NSJSON Serlizeitaion and serlize your value
                                      NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                      NSLog(@"Data = %@",text);
                                  }

                                  dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"data"];

                                  NSArray *pages = [dictionary objectForKey:@"pages"];
                                  NSLog(@"%@", pages);

                                  NSArray *IDArray = [dictionary objectForKey:@"docs"];
                                  for (NSDictionary *Dict in IDArray)
                                  {


                                      NSMutableDictionary *temp = [NSMutableDictionary new];
                                      [temp setObject:[Dict objectForKey:@"_id"] forKey:@"_id"];
                                      [temp setObject:[Dict objectForKey:@"created"] forKey:@"created"];


                                      NSMutableDictionary *currentaddress = [Dict objectForKey:@"currentLocation"];

                                      if ([[currentaddress allKeys] containsObject:@"address"]) {

                                          [temp setObject:[currentaddress objectForKey:@"address"] forKey:@"address"];

                                      }

                                      NSMutableDictionary *destination = [Dict objectForKey:@"destination"];

                                      if ([[destination allKeys] containsObject:@"address"]) {

                                          [temp setObject:[destination objectForKey:@"address"] forKey:@"address1"];

                                      }



                                      [getdetail addObject:temp];
                                      NSLog(@"%@", getdetail);
                                  }

                                  if (getdetail.count>0)
                                  {
                                      [_historytable reloadData];
                                  }

                              }];

[task resume];



    }





- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];

   }


   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
return 1;    //count of section
    }


   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {

if (_currentPage == 0) {
    return 1;
     }

if (_currentPage < totalpage) {
    return getdetail.count+1;
}
return getdetail.count;
   }

   - (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
 HIstoryTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.bookid.text = [[getdetail objectAtIndex:indexPath.row] objectForKey:@"_id"];
cell.currentlocation.text =[[getdetail objectAtIndex:indexPath.row] objectForKey:@"address"];
cell.destination.text = [[getdetail objectAtIndex:indexPath.row] objectForKey:@"address1"];
cell.created.text = [[getdetail objectAtIndex:indexPath.row] objectForKey:@"created"];
cell.createddate.text = [[getdetail objectAtIndex:indexPath.row] objectForKey:@"created"];

return cell;
     }

1 个答案:

答案 0 :(得分:0)

尝试在tableView:willDisplayCell:forRowAtIndexPath:上查看您的情况。

在显示单元格_currentPage单元格时添加getdetail.count - 1,然后再次调用API。