When to use UICollectionView instead of UITableView?

63,426

Solution 1

That depends on the requirements. How the application flows determines which type of UI to integrate into the application.

People mainly use the UICollectionview for creating types of UIs with multiple images shown in a grid. This would have complex logic using UITableView, but with UICollectionview, it would be easy.

When using UICollectionview, you don't need to set buttons with tags or other things by getting selected items values. You can simply get -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath and in UITableViewDelegate:

`-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath`

You get the selected row instead of the item, so for creating grid or modified items, using UICollectionview is best.

For the listing details of each item, people use UITableView because it shows more info on each item.

Apple Docs:

UICollectionView Class Reference

The UICollectionView class manages an ordered collection of data items and presents them using customizable layouts. Collection views provide the same general function as table views except that a collection view is able to support more than just single-column layouts. Collection views support customizable layouts that can be used to implement multi-column grids, tiled layouts, circular layouts, and many more. You can even change the layout of a collection view dynamically if you want.

UITableView Class Reference

A table view displays a list of items in a single column. UITableView is a subclass of UIScrollView, which allows users to scroll through the table, although UITableView allows vertical scrolling only. The cells comprising the individual items of the table are UITableViewCell objects; UITableView uses these objects to draw the visible rows of the table. Cells have content—titles and images—and can have, near the right edge, accessory views. Standard accessory views are disclosure indicators or detail disclosure buttons; the former leads to the next level in a data hierarchy and the latter leads to a detailed view of a selected item. Accessory views can also be framework controls, such as switches and sliders, or can be custom views. Table views can enter an editing mode where users can insert, delete, and reorder rows of the table.

Solution 2

Here's my criteria:

  • If a UITableView can do it, use it

  • If a UITableView needs lots of code to do it or can't do it at all, use UICollectionView.

You have to consider the restrictions on UITableView before making a decision: It's a single column. And you can only customize the cells, but not section backgrounds and such. So if you have a straight-up list of things with no extra frills - that looks like a bog standard iOS view, basically - then use UITableview. If you have custom insets, or a border around each section, use UICollectionView.

I'm actually considering UICollectionView for all things simply because it's very expensive when you start developing your view as a table view, then later find out it can't do that one thing that you need it to do. 1st hand experience ;)

Edit after even more experience with the two: Disregard that last paragraph. UICollectionView requires a lot of boilerplate code to make it work like a UITableView. Use UICollectionView only when really needed. ;)

Solution 3

For simple lists and forwards/backwards navigtaion, use UITableView.

If you need a high degree of customisability, use UICollectionView.

Generally speaking, in software development, it's best to choose the approach which represents "The Simplest Possible Thing".

EDIT: As of iOS 14, UICollectionView can now do lists as well and is now the recommended approach. See this session from WWDC20 for more information and implementation details: https://developer.apple.com/videos/play/wwdc2020/10026/

Solution 4

According to my point of view main difference between collectionView and tableView is that

TABLEVIEW --> show list of items in only one column.

COLLECTION-VIEW -->show list of items in multiple column.

Hope it will help you.

Solution 5

If you choose UITableView for iPhone, make sure you have considered your iPad strategy first. If you want an iPad-specific layout, you may want that single-column layout to become a grid.

Share:
63,426

Related videos on Youtube

wz366
Author by

wz366

In the Cloud. Worked at AWS, currently AZURE.

Updated on June 26, 2020

Comments

  • wz366
    wz366 almost 4 years

    I found that UICollectionView is like an upgraded version of UITableView introduced in iOS6, but when should I choose UICollectionView instead of UITableView?

    There are still Apps using UITableView, if UICollectionView can do anything UITableView can do , why people still use UITableView? Is there a difference as far as performance is concerned?

    Thanks!

  • Popeye
    Popeye about 10 years
    Whilst I agree the way this has been put seems more like an opinion and I think it should just be a comment not an answer.
  • thatzprem
    thatzprem over 9 years
    I think in terms of extendibility - Collection view scores more!!
  • rak appdev
    rak appdev over 7 years
    Perfect answer I was looking for! This should be voted more!
  • vir us
    vir us almost 7 years
    I'm wondering why one ever need to use tableview when collectionview can serve all the needs without additional complexity but provides flexibility (if a customer ever needs to extend layout to more than one column, it would be pretty straight forward to do that with collectionview). I was always using tableviews by default but now I doubt if that makes sense to switch to collectionviews instead. Am I missing something?
  • Nitin Gohel
    Nitin Gohel almost 7 years
    its all based on main requirement. Collection view introduce late and till then all dev use table to make grid and that kind of stuff. but now a days collectionview is most populor to use for making layout like tableview. as well so i dont think its a bad idea to use collectionviews
  • aero
    aero over 6 years
    Disclosing your personal experience and recommendations is very helpful!
  • ssowri1
    ssowri1 over 6 years
    Thank you for your explanation.
  • Stephen J
    Stephen J over 5 years
    I definitely chuckled reading this, well-said! UITableView is just as customizable as anything, except it will crash if you add animations that offset the queuing. I would recommend your answer to go with the Animations guide, and some digging to personally learn how many internal views exist so you can refresh them, and the "I can't make the separator line invisible" issue you eventually find won't surprise anyone. Hidden views in scrollView, UIButton, and tableView throw most customizations off until you know they're hidden in the things
  • Ben Stoller
    Ben Stoller about 2 years
    TableViews also allow for multiple selections.
  • Matthew Cawley
    Matthew Cawley about 2 years
    @BenStoller - TableViews don't respond to selections the same way a CollectionView does - As standard, a TableView accepts one selection via didSelectCell. Multiple selections that you see in features such as multiple item delete are custom built and not offered out of the box.