Search code examples
iosobjective-cuitableviewnsmutablearrayscroll-paging

TableView scroll paging with JSON


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.)


Solution

  • 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];