2017-01-10 125 views
0

我在一個視圖控制器中有三個選項卡。我用UIScrollView來滾動頁面(ViewControllers)。在第一個選項卡中,我有一個UITableView,當我滾動到底部時(非常最後一個索引或對象),UITableView完全上移。我試過這個停止TableView從底部移動頂部

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    let height = scrollView.frame.size.height 
    let contentYoffset = scrollView.contentOffset.y 
    let distanceFromBottom = scrollView.contentSize.height - contentYoffset 
    if distanceFromBottom < height { 
     print(" you reached end of the table") 

     if self.tableView.contentSize.height > self.tableView.frame.size.height { 
      let offset = CGPoint(x: CGFloat(0), y: CGFloat(self.tableView.contentSize.height - self.tableView.frame.size.height)) 
      self.tableView.setContentOffset(offset, animated: true) 
     } 
    } 
} 

這不起作用。任何人都可以告訴我如何解決這個問題?

感謝提前

+0

請把這個 –

+0

的一些代碼,如果我寫這篇文章的viewDidLoad其移動加載頁面最下方,如果self.tableView.contentSize.height> self.tableView.frame.size.height { 令偏移= CGPoint(X: CGFloat(0),y:CGFloat(self.tableView.contentSize.height - self.tableView.frame.size.height)) self.tableView.setContentOffset(offset,animated:true) } – User

+0

當您到達最後一個索引只需爲此調用scrollToRowAtIndexPath方法並將0作爲索引。它將解決您的問題 –

回答

-1

幫助如果你想表視圖滾動到底:

tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.bottom, animated: true) 

,並刪除滾動視圖代表。

+0

我需要在哪裏寫這段代碼,你可以告訴我 – User

+0

何時你想要滾動到底部。像視圖一樣加載 –

+0

indexPath將是表格的最後一個索引。 –