// to keep track of what is the next page to load @property (nonatomic, assign) int currentPage; // to keep the objects GET from server @property (nonatomic, strong) NSMutableArray *myList;
// init table list self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds]; self.tableView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin| UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; __weaktypeof(self) weakSelf = self; // refresh new data when pull the table list [self.tableView addPullToRefreshWithActionHandler:^{ weakSelf.currentPage = initialPage; // reset the page [weakSelf.myList removeAllObjects]; // remove all data [weakSelf.tableView reloadData]; // before load new content, clear the existing table list [weakSelf loadFromServer]; // load new data [weakSelf.tableView.pullToRefreshView stopAnimating]; // clear the animation // once refresh, allow the infinite scroll again weakSelf.tableView.showsInfiniteScrolling = YES; }];
// load more content when scroll to the bottom most [self.tableView addInfiniteScrollingWithActionHandler:^{ [weakSelf loadFromServer]; }]; }
// if no more result if ([[responseObject objectForKey:@"items"] count] == 0) { self.tableView.showsInfiniteScrolling = NO; // stop the infinite scroll return; } _currentPage++; // increase the page number int currentRow = [_myList count]; // keep the the index of last row before add new items into the list
// store the items into the existing list for (id obj in [responseObject valueForKey:@"items"]) { [_myList addObject:obj]; } [self reloadTableView:currentRow];
// clear the pull to refresh & infinite scroll, this 2 lines very important [self.tableView.pullToRefreshView stopAnimating]; [self.tableView.infiniteScrollingView stopAnimating];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { staticNSString *cellIdentifier = @"MyListCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } // minus 1 because the first row is the search bar id item = [_myList objectAtIndex:indexPath.row];