2013-02-18 85 views

回答

28

我相信這個問題不會有任何簡單的解決方案。可能有人可能會編寫一個單獨的庫來實現此行爲,並且一旦您在tableview中緩存數據,它會導致更多的複雜性。

但讓我們分解這個問題,這可能會幫助你實現你想要的。我用下面的代碼在應用程序中添加更多的行:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height; 
    if (endScrolling >= scrollView.contentSize.height) 
    { 
     NSLog(@"Scroll End Called"); 
     [self fetchMoreEntries]; 
    } 
} 

或者你可以做這樣的事情:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height) 
    { 
     if (!self.isMoreData) 
     { 
      self.MoreData = YES; 

      // call some method that handles more rows 
     } 
    } 
} 

你一定見過,因爲我已經在上面描述了許多問題,這些方法當然不是你要求的。但是你可以做的是當上面的代碼正在處理加載更多的數據時,你可以添加一個子視圖並顯示類似於UIRefreshControl提供的幾個圖像。在子視圖中添加圖像以顯示爲進度,直到上面的代碼得到執行。首頁這有助於。

順便說一下,我建議你不要這樣做,因爲它只會浪費你的時間來製作更小的東西,除非你只是爲了學習目的而做。

4

實際上免費的Sensible TableView框架確實提供了這種功能。您指定批號並自動獲取數據,並將最後一個單元格顯示爲「加載更多」單元格。一旦你點擊該單元格,它將自動加載下一批,等等。值得一看。

+0

請聲明您聯繫這個框架(如果有的話)這一刻閱讀它可能是垃圾郵件。 – ChrisF 2013-07-11 12:51:08

+1

不以任何方式加入!只是一個巨大的粉絲! – Matt 2013-07-11 16:15:23

+0

我想到了 - 但其他人可能不會。 – ChrisF 2013-07-11 16:22:53

10

您可以使用UIView自定義您的refreshControl底部。創建UIView並將其作爲footerView添加到UITableView。

UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]; 

[footerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"refreshImage.png"]]]; 

tableView.tableFooterView = footerView; 

隱藏它:tableView.tableFooterView.hidden = YES;

實現UIScrollView的委託 - 將數據添加到的tableView保存當前由CGPoint offset = tableView.contentOffset;

呼叫reloadData那麼先前設置的偏移之前

(void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height) 
    { 
      tableView.tableFooterView.hidden = NO; 
      // call method to add data to tableView 
    } 

} 

保存的偏移回的tableView [tableView setContentOffset:offset animated:NO];

這樣您就可以感受到底層添加的新數據。

+0

任何人都試過嗎?它工作嗎? – Ricardo 2013-10-29 12:05:59

+1

我在我的iOS 7應用程序上使用它,它似乎工作。我實際上是將我的'tableFooterView'設置爲'UIRefreshControl'視圖 – hunteros 2014-05-09 23:46:28

+1

抱歉,'UIActivityIndi​​catorView'不是'UIRefreshControl'! – hunteros 2014-05-10 00:25:50

8

我最近在最近遇到同樣的問題,並且爲UIScrollView類寫了一個類。這裏是CCBottomRefreshControl

+0

具有ReactiveCocoa依賴性,所以它沒用。 – 2014-07-03 13:18:22

+1

爲我完美工作。所有使用UIScrollViewDelegate方法的方法在性能方面都很痛苦。此方法使用默認的UIRefreshControl元素,比我遇到的其他任何方法都要好得多。 – marcelosalloum 2014-12-18 16:31:21

+0

但我遇到問題。加載數據後,我無法刪除UIRefreshcontroller。 [refreshController endRefreshing]什麼都不做。 – 2015-02-06 10:44:44

2

對於UITableView的,我建議採取下列措施:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    // if this is the last row that we have in local data 
    // and we didn't reach the total count of rows from the server side 
    if (count == indexPath.row+1 && count < total) 
    { 
     // .. fetch more rows and reload table when data comes 
    } 
} 

因爲滾動視圖是緩慢的,我沒有使用滾動視圖的方法故意。它發生滾動視圖滾動,我們請求更多的數據,刷新更多的行,並滾動視圖尚未完成滾動,它仍然減速,並導致獲取更多的數據問題。

+0

請詳細解釋@Prcela – 2018-02-02 09:13:32

2

以下答案在Xcode 8和Swift 3中。

首先聲明

var spinner = UIActivityIndicatorView() 

viewDidLoad方法寫代碼如下:

spinner = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge) 
spinner.stopAnimating() 
spinner.hidesWhenStopped = true 
spinner.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 60) 
tableView.tableFooterView = spinner 
現在終於 override

scrollViewDidEndDragging委託方法有以下幾點:

override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { 
     let offset = scrollView.contentOffset 
     let bounds = scrollView.bounds 
     let size = scrollView.contentSize 
     let inset = scrollView.contentInset 

     let y = offset.y + bounds.size.height - inset.bottom 
     let h = size.height 

     let reloadDistance = CGFloat(30.0) 
     if y > h + reloadDistance { 
      print("fetch more data") 
      spinner.startAnimating() 
     } 
} 
+0

這是今年最正確的答案。我一直在使用CCBottomRefreshControl,直到我必須將我的項目轉移到3.0,並且它開始在任何地方崩潰。這是一個更好的解決方案。謝謝! – 2017-04-26 13:41:27

+0

男人,你救了我的一天。這是swift 3的最佳解決方案 – 2017-12-06 15:11:09

相關問題