iosobjective-cpaginationtableview

How to switch pages in API?


I have an API, which has to be implemented in a table view. The API has multiple pages. What method should I implement so that as the tableview reload's I get data from next page and so on.

Note: the API is in json, in which there is an array with dictionaries as it's elements :-

page =1 [{key: value, key: value, key: value}, {key: value, key: value, key: value}, {key: value, key: value, key: value}, .....}]

page =2 [{key: value, key: value, key: value}, {key: value, key: value, key: value}, {key: value, key: value, key: value}, .....}]


Solution

  • Hard to say exactly since the json you've posted isn't valid, but assuming 'page=1' is a key with the value being the data for that page, it should be pretty straight forward:

    @property (nonatomic, assign) NSInteger currentPage;
    @property (nonatomic, strong) NSDictionary *json;
    @property (nonatomic, readonly) NSArray *pageData;
    
     - (NSArray *)pageData {
        return [self.json objectForKey:@(self.currentPage)];
    }
    

    Then you just need to make sure to adjust currentPage value when the page changes, and that your table uses pageData for its data.