我已經創建了一個tableView連接到api。我試圖實現一個加載更多的功能,當你到達tableView的底部時,它會進行新的api調用。我已經創建了這個功能,但是當api/json文件中沒有任何對象時。當我到達底部時,它似乎非常滯後,這是怎麼回事?檢測tableView是在底部滯後
當我在屏幕底部檢測並做了API調用
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功能。我甚至創建了一個isAPICalling變量,它確保它不會多次運行api。
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
}
https://github.com/vlasov/CCBottomRefreshControl(objc project) –
您是否有任何想法是否可以在swift項目中使用?使用橋樑? –