2016-06-21 18 views
-1

こんにちは、私はpoplation uitableivewフォームjsonデータです。私は私の応答の中に複数のページを持っているので、私は読書を追加する必要があります。私はすでにそれを実装しようとしましたが、行数メソッドの条件をチェックすると、空の配列の境界を超えてエラーインデックス0が返されます。jsonデータを使用してUITableViewでページネーションを実装する方法。

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; 
    } 

答えて

0

tableView:willDisplayCell:forRowAtIndexPath:にあなたの状態をチェックしてみます。

セルgetdetail.count - 1セルを表示してAPIを再度呼び出すと、_currentPageのように追加されます。

+0

あなたのお返事ありがとうございます。私はその問題を解決しましたが、現在、ロードセルメソッドでdataSourceからセルを取得できませんでした。 –

関連する問題