TableView scroll paging with JSON

987 views Asked by At

I'm newbie in objective -c. I have a problem;

I'm getting and parsing from a JSON and writing them to a tableview with array. This is clearly and nice result.

But i want to get second page when i scroll bottom. I read tones of documents. But i can't pass second page to tableView.

Here is summary of my codes;

I am calling JSON get and make array method with page 0. (No Problem)

- (void)viewDidLoad
{
    [self takeList:0];
}

My JSON get and make array method. (Problem is here)

-(void)takeList: (NSUInteger) page
{
    NSString * urlStr = [NSString stringWithFormat:@"http://www.EXAMPLE.com/json/main.json"];
    NSURL * url = [NSURL URLWithString:urlStr];
    NSData * data = [NSData dataWithContentsOfURL:url];
    if (page == 0) {
        array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    }else{
        new_array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *ar1 = [array mutableCopy];
        NSMutableArray *ar2 = [new_array mutableCopy];
        [ar1 addObject:ar2];
        [self.myTableView reloadData];
    }
}

My scroll to call method (No Problem)

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{

    NSInteger currentOffset = scrollView.contentOffset.y;
    NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;

    if ((currentOffset > 0) && (maximumOffset - currentOffset) <= 140) {
        pages++;
        [self takeList:pages];
    }

}

And finally cellForRowAtIndexPath using this array.

Please help me for "How to send new page to a tableView?".

Thanks (and sorry for my bad English.)

2

There are 2 answers

2
Bergasms On BEST ANSWER

This code

 new_array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSMutableArray *ar1 = [array mutableCopy];
    NSMutableArray *ar2 = [new_array mutableCopy];
    [ar1 addObject:ar2];
    [self.myTableView reloadData];    

Is not going to do anything useful. This is not how you concatinate arrays in objective-c (or any language that i can think of. Instead, you need to replace it with the following

 new_array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    array = [array arrayByAddingObjectsFromArray:new_array];
    [self.myTableView reloadData];    
0
ninja_iOS On

As I see you have two problems here: You are using NSArray called "array" to as data source when your page is 0. When you are getting next pages you are using ar1.

Make "array" as NSMutableArray and do the following:

-(void)takeList: (NSUInteger) page
{
    NSString * urlStr = [NSString stringWithFormat:@"http://www.EXAMPLE.com/json/main.json"];
    NSURL * url = [NSURL URLWithString:urlStr];
    NSData * data = [NSData dataWithContentsOfURL:url];
    array = [array arrayByAddingObjectsFromArray:[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]];
self.myTableView reloadData];
}

Of course you have to initialize your array first somewhere else. Also I would do it with async request and check if any data was downloaded and there were no parsing errors.