Detect when tableView is at the bottom

I have created a tableView which is connected to the api. I am trying to implement loading more functionality when it creates a new api call when you reach the bottom of the table. I created this functionality but when there are no more objects in the api / json file. he seems to be very lagging behind when i get to the bottom how did it go?

detecting when i am at the bottom of the screen and make an api call

func scrollViewDidScroll(scrollView: UIScrollView!) {



    var height = scrollView.frame.size.height;
    var contentYoffset = scrollView.contentOffset.y;
    var distanceFromBottom = scrollView.contentSize.height - contentYoffset;

    if distanceFromBottom < height
    {
        if isApiCalling == false {
            getRecent("URL/recent.php?lastid=\(lastObjectIndex)&limit=\(numberOfRecordsPerAPICall)")
            self.tableVIew.reloadData()
        }
    }
}

      

GetRecent function. I even created an isAPICalling variable that makes sure it doesn't start the api multiple times.

func getRecent(url: NSString) {

    isApiCalling = true
    let recentUrl = NSURL(string: url as String)
    var recentRequest = NSURLRequest(URL: recentUrl!)
    var recentData = NSURLConnection.sendSynchronousRequest(recentRequest, returningResponse: nil, error: nil)

    let jsonArray = JSON(data: recentData!)
    for (key: String, subJson: JSON) in jsonArray {
        // Create an object and parse your JSON one by one to append it to your array
        var newNewsObject = News(id: subJson["id"].intValue, title: subJson["title"].stringValue, link: subJson["url"].stringValue, imageLink: subJson["image_url"].stringValue, summary: subJson["news_text"].stringValue, date: getDate(subJson["date"].stringValue))



        recentArray.append(newNewsObject)
    }

    lastObjectIndex = lastObjectIndex + numberOfRecordsPerAPICall
    isApiCalling = false


}

      

+3


source to share


1 answer


You can add a footer to the tableview section if the footer gets a load and then call the next set with API calls (load footer only if flag

== - 1)

When requesting the next set, you can set a flag (for example flag

= 1), this call has already been made, so if the user scrolls up and down, the request will not be sent (if flag

== 1).

In response, return the flag to normal mode (eg flag

= 0) and check the answer, if there is no more data, set the flag to not load the footer (eg flag

= - 1).



PS Make sure it flag

is 0 when you first view downloads.

May solve your problem this way.

HTH, enjoy coding!

+1


source







All Articles