UITableView load more when scrolling to bottom

前端 未结 6 445
深忆病人
深忆病人 2020-12-08 00:28

I would like to load more data and create additional cells when the user scrolls to the bottom of the table. I\'m using JSON data and MySql.

func dataJsonFro         


        
相关标签:
6条回答
  • 2020-12-08 01:01

    You need one very handy delegate method:

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell , forRowAtIndexPath indexPath: NSIndexPath) {
    
    }
    

    This method gives your the current displayed row. Use the indexPath it provides to determine when you are nearing the bottom of the table. Then get more JSON and reload the table.

    0 讨论(0)
  • 2020-12-08 01:03

    you have to use this method

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
                let lastElement = dataSource.count - 1
                if indexPath.row == lastElement {
                    // handle your logic here to get more items, add it to dataSource and reload tableview
                    }    
            }
    
    0 讨论(0)
  • 2020-12-08 01:03

    Use this library. https://github.com/samvermette/SVPullToRefresh

    This will full fill your requirements. You just need to change page number or timestamp every time, when new web-service will be called.

    0 讨论(0)
  • 2020-12-08 01:09

    Its Called pagination to tableview. Pagination is used to show large data on a table in an effective way. Here's you can get nice tutorial for objective-c and for swift look at this Question.

    And you can download example in swift from Here.

    0 讨论(0)
  • 2020-12-08 01:13

    Xcode 8, Swift 3

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let lastElement = dataSource.count - 1
        if !loadingData && indexPath.row == lastElement {
            indicator.startAnimating()
            loadingData = true
            loadMoreData()
        }
    }
    
    0 讨论(0)
  • 2020-12-08 01:23

    i used this method and it's works for me

     func scrollViewDidScroll(scrollView: UIScrollView) {
        let offsetY = scrollView.contentOffset.y
        let contentHeight = scrollView.contentSize.height
    
        if offsetY > contentHeight - scrollView.frame.size.height {
    
    
            loadMoreDataFromServer()
            self.tableView.reloadData()
        }
    }
    
    0 讨论(0)
提交回复
热议问题