Apollo
Apollo

Reputation: 9054

Grid of images iOS

I'm developing a simple gridview to show images. The grid will have 3 columns and N number of rows. I'm considering the two following approaches:

1) Use a UITableView with custom cells, each containing 3 UIImageViews.

2) Using a UICollectionView

I'm more comfortable with #1 because I'm used to implementing UITableViews, but will either be more efficient than the other?

Upvotes: 0

Views: 614

Answers (2)

Mihir
Mihir

Reputation: 992

Introduction

In terms of processing efficiency, I don't believe there is a significant difference between either option. However, in terms of intuitiveness and simplicity, the UICollectionView is the better of the two options. If you feel comfortable with a UITableView, then picking up a UICollectionView is not at all difficult; Apple has done a great job in standardizing the interfaces between the two. Here are some advantages of using a UICollectionView that I thought of off the top of my head.

UICollectionView Advantage: Future functionality is easier

A grid of images seems to be the starting point of an image gallery; I as a user would want to be able to tap on an image and see it in detail. Perhaps you'll implement something like this in the future. With the UICollectionView, all you have to do is implement:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

which is part of the UICollectionViewDelegate protocol implementation. In this method, you know exactly the image that is being referenced (you know the indexPath) However, for the UITableView, if you implement the analogous method, you'll know which UITableViewCell was selected, but won't know which one of the images was selected without a little more work. Thus, displaying that image fullscreen becomes that much harder.

UICollectionView Advantage: Alterations are simpler

Now consider this: suppose tomorrow you decide that three images in a row aren't good enough; you're going for two. With a UICollectionView, all you have to really do is modify your storyboard. With a UITableView, you have to modify the cell to have two images, then you have to modify the cell's attributes to have only two image views rather than three, and then you have to remove the third asynchronous call to get the image from a server from the

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

method. That's a lot more moving parts than the previous option.

Conclusion

These were just two reasons I came up with off the top of my head. In general when dealing with the term "efficiency", I try to ask myself "what could possibility change in the future, and how can I optimize to make that change as easy as possible?" In our case here, the UITableView option is a decent option if nothing really changes in the long run, but hey, maybe you'll add functionality, or decide to change your layout; thus really the most efficient option, in that sense, is the UICollectionView one. Good luck!

Upvotes: 2

NeverHopeless
NeverHopeless

Reputation: 11233

Your case matches to a row-wise reusability, so i would prefer UITableView, but keep this in mind that if your images are not the part of your resources and you always download it from server then your tableView might start hanging during scroll (unless it is handled very carefully by using threads). For that case you also have an option to use UIScrollViewwith custom views (which holds UIImageView and take responsibility to download them).

Upvotes: 0

Related Questions