Search code examples
iosdatabaseuicollectionviewuicollectionviewcell

How do I organise UICollectionViewCells according the server data they are storing?


I have cells which display images that a user has taken and added to the UICollectionView. On the API server, these images are organised by the timestamp (see below), which means that in the app, the first cell displayed is the most recent image uploaded to the server.

On the server, it looks like this:

results: [
{
i_id: "28",
title: "image title",
description: "Picture",
longitude: "0.000000",
latitude: "0.000000",
filename: "http://server.com/images/image.jpg",
timestamp: "12:34:04",
n_likes: "0",
n_comments: "1",
images: {

}
},

{
i_id: "21",
title: "another image title"
description: "another Picture",
longitude: "0.000000",
latitude: "0.000000",
filename: "http://server.com/images/image.jpg",
timestamp: "12:20:49",
image: "",
n_likes: "2",
n_comments: "4",
images: {

}
}
]

I am able to get each image’s n_comments data into an array without problems. I want the array to count the number at each object at index and determine which would be displayed in the UICollectionView first, according to the number that the object contains (if that makes sense).

Would this be something that I’d have to manage on the server, or can I do it within the app’s code? I’m guessing that it has to do with UICollectionViewLayout, so that’s where I’ll be attempting to get it to work in the meantime.

Any advice would be much appreciated!


Solution

  • You could do this on either end, but you don't need to do anything with UICollectionViewFlowLayout. On the app end, you could sort your array with sortedArrayUsingDescriptors: with a descriptor of sortDescriptorWithKey:ascending:, passing n_comments as the key.