When to use UICollectionView instead of UITableView?
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.
UITableViewController VS UICollectionView
[Your question is a possible duplicate of this one here: When to use UICollectionView instead of UITableView? but I decided to give you a short overview here.]
That's kind of true. It always depends on your use case and UI. In the end you can also use a UICollectionView
to build your Table. It's all just easier one way sometimes. And sometimes not possible with one of them.
This is how Apple would describe it:
UITableView Displays hierarchical lists of information and supports selection and editing of the information.
Apple Developer Documentation - UITableViewUICollectionView Manages an ordered collection of data items and presents them using customizable layouts.
(Apple Developer Documentation - UICollectionView)
To answer your question:
I would mostly use a UITableView
because they do a great job in displaying lists of information. But if you see it's going to be more complex, use UICollectionView
. There is a good YouTube video series on how to customize them to build a twitter news feed. It's a good example of how to use it and when to use it. Check it out
To sum up:
- Use
UICollectionView
for more complex and advanced things where you need a lot of customization. - Use
UICollectionView
for things that are placed in a grid with for example three pictures in every row. - Use
UITableView
for simple and advanced lists but not necessarily if you need a feed like twitter uses (to point out an example).
Keep in mind that you can use custom cells (with or without XIB files) in a UITableView
!
However just choose the way you are most comfortable with.
UITableView vs UICollectionView vs UIScrollView?
Both CollectionView & TableView are basically subclasses of UIScrollView.But as compared to UIScrollView here you are provided with proper methods to provide your dataSource & delegates to handle operations user performs on data. Along with this you are provided predefined layout classes.
Now to choose between them completely depends on your UI Requirements. Suppose you want to display just a list of items with a simple UI go with TableView.If you want a custom Layout like a grid or like the one you see in Apple's photo's app CollectionView is the choice.
if you have a complex UI & you have no idea about Custom CollectionView layout classes go with scrollView.
Why is Facebook using UICollectionView instead of UITableView for their News Feed
Facebook feed is indeed a UICollectionView and this is mostly for the flexibility it offers moving forward.
With our custom layout, we can move around / animate the cells fairly cleanly. You can attempt to do with a UITableView, but it requires some substantial hacks that we would rather avoid.
When we migrated from UITableView to UICollectionView we also noticed an uptick in scroll performance. Unfortunately I haven't been able to identify the exact call that got faster.
What is the difference of implementation between UITableView & UICollectionView when using Xib?
UICollectionView requires a UICollectionViewLayout
in order to render. When creating a collection view via the storyboard this is already set with a default value.
You can initialise it like this:
let flowLayout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
Flow layout
A concrete layout object that organizes items into a grid with optional header and footer views for each section.
The flow layout is one of the default/built in options and is useful for creating a 'bookshelf' style grid in which the columns fill left to right and then top to bottom (unless you change the width).
There is a good tutorial on the Ray Wenderlich website which explains more about collection views and their layouts
Related Topics
Randomly Choosing an Item from a Swift Array Without Repeating
How to Change Height of Uitableviewcell Real-Time
Over the Air (Ota) iOS IPA File Distribution for Public
Change Uitextfield and Uitextview Cursor/Caret Color
Making an Array of Integers in iOS
Logging Method Signature Using Swift
How to Turn Flashlight on and Off in Swift
How to Properly Change My Status Bar Style in Swift 2/ iOS 9
Bridge Google Drive API to Swift
Variable Used Before Being Initialized in Function
Install .Ipa to iPad with or Without Itunes
Adding Background Image to Uilabel