当UITableView已滚动到底部时检测到

问题描述:

下面的帖子仍然是检测UITableView的实例何时滚动到底部[在Swift]中,还是被更改(如:改进后)的可接受方式?当UITableView已滚动到底部时检测到

Problem detecting if UITableView has scrolled to the bottom

谢谢。

试试这个

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 tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height) { 

     //you reached end of the table 
    } 
+3

感谢@ Anbu.Karthik找到。我很可能会这样做。我非常害怕这样的原始方法仍然需要在2016年使用,但它是如此。什么阻止工程师引入“func scrollViewDidScrollToBottom”功能? –

+0

@TheMotherofJosephBeuys - 我不明白你的观点,有什么方法可用在iOS中'func scrollViewDidScrollToBottom“' –

+0

不要担心@ Anbu.Karthik,我只是呻吟 –

斯威夫特3

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    if indexPath.row + 1 == yourArray.count { 
     print("do something") 
    } 
} 
+1

该解决方案仅在有1个区段时才有效,当有更多区段时,那么需要累积左边的行 – kjoelbro

+1

这似乎是对我最好的答案 –

+1

@kjoelbro您可以方便地获得整个'indexPath'。您可以轻松地确定tableview的结尾位置 –