簡體   English   中英

在 UITableView 中從網絡預加載數據

[英]Preloading data from network in UITableView

從網絡加載數據時,我有一個支持分頁的頁面。

我正在使用 UITableview 來顯示列表。

當用戶滾動到當前頁面的末尾附近時,我想預加載下一頁。

例如,每頁有 10 個項目。 當屏幕上顯示第 8 項時,我將立即開始加載下一頁。

UITableView 的哪個委托方法是這個任務的最佳選擇?

試試下面的方法,就可以知道顯示單元格了

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
    forRowAtIndexPath:(NSIndexPath *)indexPath
    {
//      Webservice Call for next 10 display data
    }

討論 表格視圖在它使用單元格繪制一行之前將這個消息發送給它的委托人,從而允許委托人在單元格對象顯示之前對其進行自定義。 此方法使委托有機會覆蓋表視圖之前設置的基於狀態的屬性

UITableView 是 UIScrollView 的子類,因此您可以使用它委托方法。 我建議你使用這兩個:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    if (!decelerate) {
         NSSet *visibleRows = [NSSet setWithArray:[self.view.tableView indexPathsForVisibleRows]];

    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    NSSet *visibleRows = [NSSet setWithArray:[self.view.tableView indexPathsForVisibleRows]];

}

在獲得visibleRows並預加載數據后檢查您的子句,例如,如果 NSSet 中的最后一個indexPath.row等於您的數據源數組計數(或等於 [array count] - some_number_you_want )

要存檔,當 tableView 開始滾動時,您可以在此處實現預加載邏輯:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    NSSet *visibleRows = [NSSet setWithArray:[self.view.tableView indexPathsForVisibleRows]];
// your update code here
}

希望這可以幫助。

UITableViewDelegate添加這個方法:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{   
    CGFloat height = scrollView.frame.size.height;

    CGFloat yOffset = scrollView.contentOffset.y;

    CGFloat distanceFromBottom = scrollView.contentSize.height - yOffset;

    if(distanceFromBottom < height) 
    {
        NSLog(@"end of the table and call web service or load more data");
    }
}

使用UITableViewDataSourcePrefetchingUICollectionViewDataSourcePrefetching從網絡預加載數據,並在向用戶顯示時准備好單元格

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM