2015-07-21 22 views
3

語境做的時候用戶滾動到最後tableViewCell

  • 構建類型的新聞源

問題

  • 我需要加載更多帖子動作當用戶滾動到最後一個表格單元格
  • 我不知道從哪裏開始,如何實施,凡在代碼
  • 所以基本上我需要打電話到服務器,下載的帖子時,用戶滾動到最後一個表視圖,追加的職位,以目前的表視圖
  • 底部

下面是相關的代碼塊(我想!)

class GeneralPostAreaController: UIViewController { 
     ... 
     extension GeneralPostAreaController: UITableViewDataSource { 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return PostsCollection.postsFromParse.posts.count 

    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as! PostTableViewCell 
     cell.postImage.image = PostsCollection.postsFromParse.posts[indexPath.row].image 
     cell.postName.text = PostsCollection.postsFromParse.posts[indexPath.row].name 
     cell.postText.text = PostsCollection.postsFromParse.posts[indexPath.row].text 

     cell.loadAudio = PostsCollection.postsFromParse.posts[indexPath.row].audioObject.parseAudioData 
     return cell 
    } 

} 

這裏是我使用從服務器(解析)獲取數據的代碼。原諒可怕的數據結構,這是我的第一個應用程序!

class ParseQueryer: NSObject{ 

//HAVE TO RESTRUCTURE 

var posts: [Post] = [] 

//this is not a very elegant way to reload table Data 
func getPosts(selectedTableView: UITableView){ 

var query = PFQuery(className: "Post") 
query.addDescendingOrder("createdAt") 
query.limit = 10 
query.findObjectsInBackgroundWithBlock {(result: [AnyObject]?, error: NSError?) -> Void in 
    self.posts = result as? [Post] ?? [] 

    selectedTableView.reloadData() 

    for post in self.posts{ 
     post.name = post["Name"] as? String 
     post.text = post["Text"] as? String 
     //println(post.text) 
     if let audioData = post["Audio"] as? PFFile { 
      audioData.getDataInBackgroundWithBlock({ 
       (audioData: NSData?, error: NSError?) -> Void in 
       if (error == nil){ 
        post.audioObject.parseAudioData = audioData 
       } 
      }) 
     } 
     if let imgData = post["Photo"] as? PFFile { 
      imgData.getDataInBackgroundWithBlock({ 
       (imageData: NSData?, error: NSError?) -> Void in 
       if (error == nil){ 
        let image = UIImage(data: imageData!) 
        post.image = image 
       } 
      }) 
     } 

    } 
} 
    selectedTableView.reloadData() 

我看了回答here但它對我來說沒有意義。

回答

1

so UITableView繼承自UIScrollView,因此您可以使用scrollview委託方法來了解何時滾動到底部。

override func scrollViewDidScroll(scrollView: UIScrollView) { 

     //1. decide on the distance from the bottom that if the user scrolls to that point you will load more results 
     let minimumTrigger = scrollView.bounds.size.height + self.triggerDistanceFromBottom 

     // 2. Now you are checking to see that the height of all the content in the scrollview/tableView.(i.e. all the cells hights stacked ontop of eachother) is greater than the minimum height for triggering a load 
     // This step is so that if you have only a few results, the loadMoreResults method won't constantly be called. 
     if scrollView.contentSize.height > minimumTrigger { 

      //3. This calculated the distance from the bottom of the scrollview. 
      let distanceFromBottom = scrollView.contentSize.height - (scrollView.bounds.size.height - scrollView.contentInset.bottom) - scrollView.contentOffset.y 

      //4. then this is the crucial check. 
      if distanceFromBottom < self.scrollTriggerDistanceFromBottom { 
       // Do your stuff 
      } 
     } 

    } 
0

您可以通過這種方法做你的負載更多的操作,如表視圖委託是滾動視圖代表:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height; 
    if (bottomEdge >= scrollView.contentSize.height) 
    { 
     // we are at the end 

     if(currentCount<pageCount) 
     { 
      if(isLoading) 
      { 
       return; 
      } 
      UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
      [spinner startAnimating]; 
      spinner.frame = CGRectMake(0, 22, 50, 44); 
      self.tblCentreEvents.tableFooterView = spinner; 

      isLoading=TRUE; 
      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ 
       [self populateDataInList]; 

      }); 


     } 

    } 
} 
0

最好的辦法是在屏幕底部的測試點並使用此方法調用當過用戶滾動(scrollViewDidScroll):

- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point 

測試靠近屏幕的底部,然後點使用它返回檢查indexPath如果indexPath是最後的R如果是,則添加行。