HiveBrain v1.2.0
Get Started
← Back to all entries
snippetswiftMinor

Image Scrolling in UITableView with Parse

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
imagescrollingwithparseuitableview

Problem

I'm trying to create an app that is scalable. One of the main features of the app is to view images from a database to a UITableView. Here is my code:

```
import UIKit

class HomePageTableViewController : UITableViewController {

private var imageObjects: [ImageParseObject] = []
private var skipNumObjects = 0
private var notificationKey = "bananasaregood"

override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 320
loadMoreImages()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "refreshData", name: notificationKey, object: nil)
}

func loadMoreImages() {
let query = PFQuery(className: "ImagePost")
query.limit = 25
query.skip = skipNumObjects
query.findObjectsInBackgroundWithBlock({
objects, error in
if error != nil {
print(error)
} else {
self.imageObjects += self.convertPFObjectsToImageParseObjects(objects!)
}
})
skipNumObjects += 25
}

func refreshData() {
tableView.reloadData()
}

func convertPFObjectsToImageParseObjects(pfObjects: [PFObject]) -> [ImageParseObject] {
var imgObjects: [ImageParseObject] = []
for i in 0.. Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return imageObjects.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! HomePageTableViewCell
let imageParseObject = imageObjects[indexPath.row]
cell.imagePost.image = UIImage(named: "default")
cell.imagePost.file = imageParseObject.image
cell.imagePost.loadInBackground()
cell.userImage.image = UIImage(named: "default")
cell.userImage.file = imageParseObject.userImage
cell.userImage.loadInBackground()
cell.timeAgo.text = imageParseObject.timeAgo!
return cell
}
}

extension HomePageTableViewController {

o

Solution

I predict that scrolling in this tableview would be choppy. The problem you are trying to solve is pretty common - for example, find a shopping app that has to display many images of products in a UICollectionView. The scrolling in these UICollectionViews is smooth even though you might be scrolling through hundreds of images. How do they do this?

Here are my suggestions.

1) Find an image caching framework to use like Kingfisher or its Objc predecessor SDWebImage.

2) Parse objects have a URL property (according to their docs).

3) When you populate the tableview, each cell gets configured with its own Parse object. Within each cell, use the image caching frameworks method for setting an image on that cell's image view with that cell's Parse object's url property.

4) Paging - if you have thousands of users, you don't want to download all 50000 at once. Inside cellForRowAtIndexPath, you should check the current row against the data source array of the table view and see if you're about to run out of data to display, and when that happens, eg (row <= data.count - 4) call your fetch method for more Parse data.

Context

StackExchange Code Review Q#116084, answer score: 2

Revisions (0)

No revisions yet.