2017-01-25 43 views
0

我加入新的數據到tableView當用戶滾動到這樣的底部....加載更多的細胞,當用戶滾動到的tableview框架的底部

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView { 
     float height = self.itemListTableView.frame.size.height; 
     float contentYoffset = aScrollView.contentOffset.y; 
     float distanceFromBottom = aScrollView.contentSize.height - contentYoffset; 
     if (distanceFromBottom < height) { 
     NSLog(@" you reached end of the table"); 
     isTableScrolledToBottom = YES; 
    }else{ 
     isTableScrolledToBottom = NO; 
    } 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"CellIdentifier"; 
    ListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell==nil){ 
     cell = [[ListViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
     //Assign values to labels here.. 

    if (isTableScrolledToBottom) { 

     if (indexPath.row == [apiMutableArray count]-1) 
     { 
      // Call the API 
      NSLog(@"Call the api"); 
      int pgn = [self.currentPageNumber intValue]; 
      pgn = pgn+1; 
      self.currentPageNumber = [NSNumber numberWithInt:pgn]; 
      NSLog(@"incremented page number : %@",self.currentPageNumber); 
      NSMutableDictionary *mutableDict = [self.poPostDict mutableCopy]; 
      [mutableDict setObject:self.currentPageNumber forKey:@"pageno"]; 
      self.poPostDict = [mutableDict mutableCopy]; 

      [self addLoaderViewAtBottom]; 
      [self loadPOData:self.poPostDict]; 
     } 
    } 
return cell; 
} 

現在,如果在表視圖中的數據是大足夠了,用戶將滾動到底部,並按預期從api完成數據提取。但是,當我過濾此表並讓我說只有一行數據時。 在這種情況下,即使我可以看到表格中只有一個單元格,如果我嘗試滾動表格,則會觸發api請求。 如何真正知道用戶是否已經滾動到其frame以上,並且最終達到scrollView's,因此只有在這種情況下我才能獲取數據。

注意: - 使用當前的技術,如果只有一行是存在的,我嘗試滾動,API調用連續命中而不停止。

+0

解決方案已經出現在堆棧上。你有沒有嘗試過任何一個? –

+0

我發現這些技術在堆棧本身。但是,如果我過濾表的解決方案似乎並不工作。 – icodes

+0

您不需要過濾必須添加數據的數據。 –

回答

0

試試這個解決方案,它總是爲我工作。

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
     let lastElement = dataSource.count - 1 
     if indexPath.row == lastElement { 
      // handle your logic here to get more items, add it to dataSource and reload tableview 
      }  
    } 

如果您仍然有疑問,然後問。

+0

我需要關於同一問題的幫助,我有一個圖像的JSON響應,它有193個數據截至目前它可以在未來增加,所以目前我想一次只顯示20個單元格,並且當用戶到達20個單元格和滾動我想顯示下面的另外20個單元格,以便圖像加載將更快。我想要facebook分頁,我不想添加pagecount增量和獲取值時,我提前調用delegate methods.thnks ...這是我的問題https://stackoverflow.com/questions/49187395/how-to- show-cells-based-on-increment-of-array-in-tableview-swift-3 –

+0

https://paste.ofcode.org/5bZuFj8uStwbzyZaqPtyj3 –

+0

現在勾選bro可以嗎? –

相關問題