我目前有一個Xcode項目,可以加載數據並將它們放入數組中,該數組將被排序並放置在一張桌子上。截至目前,當我進入viewDidLoad()部分的表格時,我有一個函數可以收集我需要的所有數據。但是爲了阻止應用程序崩潰,它加載了30個限制。我目前的問題是,一旦用戶滾動瀏覽了30個項目,我想在最初的30個(標準滾動)下面加載30個項目。我可以考慮的唯一方法是重新加載整個表,這不是很聰明。像Instagram或任何應用程序一樣向下滾動時加載
我只想要一個標準的滾動功能,您可以在Instagram,Facebook或在桌面視圖中加載數據的任何互聯網應用中看到這些功能。所以當用戶滾動時,更多的數據被添加到底部。下面我抄我的代碼我用來收集原始數據:
func findAnimalUsers() {
//STEP 1: Find users
let animalQuery = PFQuery(className: "Animals") //choosing class
animalQuery.whereKey("dog", equalTo: animalType.text!) //getting users with animal type user types
animalQuery.limit = 30 //number of users intitally showing
animalQuery.findObjectsInBackground (block: { (objects, error) -> Void in
if error == nil { //if no error
//clean up
self.animalArray.removeAll(keepingCapacity: false)
//STEP 2: Find related objects depending on query setting
for object in objects! {
self.animalArray.append(object.value(forKey: "user") as! String) //objectId of related users
}
//STEP 3: Find users
let query = PFUser.query()
query?.whereKey("objectId", containedIn: self.animalArray) //finding users
query?.addDescendingOrder("createdAt") //how to order users
query?.findObjectsInBackground(block: { (objects, error) -> Void in
if error == nil {
//clean up
self.usernameArray.removeAll(keepingCapacity: false)
self.profilePhotoArray.removeAll(keepingCapacity: false)
self.objectIDArray.removeAll(keepingCapacity: false)
//find related objects depending on query setting
for object in objects! {
self.usernameArray.append(object.object(forKey: "username") as! String)
self.profilePhotoArray.append(object.object(forKey: "profilePhoto") as! PFFile)
self.objectIDArray.append(object.objectId!)
}
} else {
print(error)
}
})
} else {
print(error)
}
})
}
我還添加了的表如何使用這些信息的代碼:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//define cell
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! AnimalsCell
//STEP 1: connect data from server to objects
cell.usernameLabel.text = usernameArray[indexPath.row]
cell.objectID = objectIDArray[indexPath.row]
profilePhotoArray[indexPath.row].getDataInBackground (block: { (data, error) in
if error == nil {
cell.profilePhoto.image = UIImage(data: data!)
} else {
print(error)
}
})
所以,當這個視圖控制器來了在viewDidLoad()中,我只有findAnimalUsers()來啓動初始加載。 如何在滾動時加載更多?
看看https://github.com/pronebird/ UIScrollView-InfiniteScroll –