首页 > 解决方案 > 滚动到底部作为分页时从 API 加载更多数据的最佳方法

问题描述

我想在 UITableView 中滚动到底部时根据页码从 API 中获取更多日期

是否使用此方法

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if (((scrollView.contentOffset.y + scrollView.frame.size.height) > scrollView.contentSize.height ) && ! isLoadingList){
        self. isLoadingList = true
        self. loadMoreItemsForList()
    }
}

或使用此方法

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    // fetch new data if user scroll to the last cell
    guard isLoadingIndexPath(indexPath) else { return }
    if self.totalItems > orders.count {
        fetchNextPage()
    }
}

标签: swift3

解决方案


滚动到底部时使用func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)方法获取更多数据。


推荐阅读