UITableView Detecting Last Cell

前端 未结 8 744
广开言路
广开言路 2021-01-31 10:46

How can I detect when a UITableView has been scrolled to the bottom so that the last cell is visible?

8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 11:24

    I had some problems with the willDisplayCell method. This works for me:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let y = scrollView.contentOffset.y/(scrollView.contentSize.height - scrollView.frame.size.height)
        let relativeHeight = 1 - (table.rowHeight / (scrollView.contentSize.height - scrollView.frame.size.height))
        if y >= relativeHeight{
            print("last cell is visible")
        }
    }
    

    Just pass this in the delegate methods, and change 'table' to your tableView.

提交回复
热议问题