Swift tableView Pagination

后端 未结 14 1373
长情又很酷
长情又很酷 2020-11-28 20:00

I have success working tableview with json parsing codes.But may have 1000 more item so need pagination when scrolling bottom side. I dont know how can i do this my codes un

14条回答
  •  独厮守ぢ
    2020-11-28 20:38

    SWIFT 3.0 and 4.0

    If you're sending the page number in the API request then this is the ideal way for implementing pagination in your app.

    1. declare the variable current Page with initial Value 0 and a bool to check if any list is being loaded with initial value false
        var currentPage : Int = 0
        var isLoadingList : Bool = false
    
    1. This is the function that gets the list example:
        func getListFromServer(_ pageNumber: Int){
            self.isLoadingList = false
            self.table.reloadData()
        }
    
    1. This is the function that increments page number and calls the API function
       func loadMoreItemsForList(){
           currentPage += 1
           getListFromServer(currentPage)
       }
       
    
    1. this is the method that will be called when the scrollView scrolls
        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            if (((scrollView.contentOffset.y + scrollView.frame.size.height) > scrollView.contentSize.height ) && !isLoadingList){
                self.isLoadingList = true
                self.loadMoreItemsForList()
            }
        }
    

    P.S. the bool isLoadingList role is to prevent the scroll view from getting more lists in one drag to the bottom of the table view.

提交回复
热议问题