Ответ 1
Кажется, вы не думаете о MVC. Ваш UITableView очень мало работает с поисковым вызовом и веб-запросом. Это просто беспокоило его источник данных, а не страницы.
Restuful API Design: Предположим, что ваш веб-запрос разработан следующим образом:
/getRecorods PAGESIZE = 20 &? PageNo = 2
Это вернет вам массив JSON. В дополнение к этому полезно использовать параметр count и ссылку на следующую страницу. Это помогает в анализе и синхронизации с веб-сервером.
как вы предотвращаете запуск API несколько раз для одних и тех же строк?
Простой флаг достаточно, чтобы избежать загрузки нескольких страниц. Просто убедитесь, что флаг доступен в основном потоке. Фактический веб-запрос должен идти в фоновом потоке.
Ниже приведен код, который необходимо поместить в ваш UITableViewController, который загружает данные
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //call http Util here to load the data httpUtil.delegate = self; //This retrieves post for first page always currentPageNumber = 1; [httpUtil getRecords:currentPageNumber]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. int retValue = 0; if(recordsArray != nil){ retValue = [recordsArray count]; } return retValue; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { } // Configure the cell using recordsArray objectAtIndex return cell; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ if(self.tableView.contentOffset.y >= (self.tableView.contentSize.height - self.tableView.bounds.size.height)) { //NSLog(@" scroll to bottom!"); if(isPageRefresing == NO){ // no need to worry about threads because this is always on main thread. isPageRefresing = YES; [self showMBProfressHUDOnView:self.view withText:@"Please wait..."]; currentpagenumber = currentpagenumber +1; [httpUtil getRecords:currentpagenumber]; } } } // you can get pageNo from tag // make sure this is called in main thread -(void)didFinishRecordsRequest:(NSArray *)results forPage:(NSInteger)pageNo{ if(pageNo == 1){ recordsArray = [results mutableCopy]; } else{ [recordsArray addObjectsFromArray:results]; } isPageRefresing = NO; [self.tableView reloadData]; } -(void)didFailedChalkBoardRequestWithError:(NSError *)error{ //If Since subsequent refresh calls(for page 2 or page 3 fails) //then undo the current page number currentpagenumber--; isPageRefresing = NO; } // HTTP Utility class -(void)getRecords:(NSInteger)pageNumber{ NSString *serverUrl = [NSString stringWithFormat:@"http://yourwebsite.com/page/%d /?json",pageNumber]; NSLog(@"fetching Stories data from server WITH URL %@",serverUrl); NSURL *url = [NSURL URLWithString:serverUrl]; storiesRequest = [ASIHTTPRequest requestWithURL:url]; storiesRequest.tag = pageNumber; [storiesRequest setDelegate:self]; [storiesRequest startAsynchronous]; }