I currently have an Xcode project that loads data and puts them in arrays that will be sorted through and placed on a table. As of now when I go to the table in the viewDidLoad() part, I have a function that will gather all of the data I need. But to stop the app from crashing it loads a limit of 30. My current problem is that once the user scrolls through the 30 items, I want 30 more to be loaded beneath the initially 30 (standard scrolling). The only way I can think about doing this is reloading the whole table which is not very smart.
I just want a standard scrolling feature that you see in apps like Instagram, Facebook, or any internet app that loads data on a tableview. So as the user is scrolling, more data is being added to the bottom. Below I have copied my code the I use to gather the initial data:
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)
}
})
}
I have also added the code of how the table uses this information:
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)
}
})
So when this view controller comes up in viewDidLoad(), I just have findAnimalUsers() in it to start the initial load. How do I load more while scrolling down???
You can use delegate method of scrollview
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
let offsetY = scrollView.contentOffset.y
let contentHeight = scrollView.contentSize.height
let scrollHeight = scrollView.frame.size.height
if offsetY >= contentHeight - scrollHeight
{
//your logic
}
}