Questions tagged [uicollectionview]

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. Available in iOS 6.0 and later

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.

When adding a collection view to your user interface, your app’s main job is to manage the data associated with that collection view. The collection view gets its data from the data source object, which is an object that conforms to the UICollectionViewDataSource protocol and is provided by your app. Data in the collection view is organized into individual items, which can then be grouped into sections for presentation. An item is the smallest unit of data you want to present. For example, in a photos app, an item might be a single image. The collection view presents items onscreen using a cell, which is an instance of the UICollectionViewCell class that your data source configures and provides.

In addition to its cells, a collection view can present data using other types of views too. These supplementary views can be things like section headers and footers that are separate from the individual cells but still convey some sort of information. Support for supplementary views is optional and defined by the collection view’s layout object, which is also responsible for defining the placement of those views.

Besides embedding it in your user interface, you use the methods of UICollectionView object to ensure that the visual presentation of items matches the order in your data source object. Thus, whenever you add, delete, or rearrange data in your collection, you use the methods of this class to insert, delete, and rearrange the corresponding cells. You also use the collection view object to manage the selected items, although for this behavior the collection view works with its associated delegate object.

Collection Views and Layout Objects

A very important object associated with a collection view is the layout object, which is a subclass of the UICollectionViewLayout class. The layout object is responsible for defining the organization and location of all cells and supplementary views inside the collection view. Although it defines their locations, the layout object does not actually apply that information to the corresponding views. Because the creation of cells and supplementary views involves coordination between the collection view and your data source object, the collection view actually applies layout information to the views. Thus, in a sense, the layout object is like another data source, only providing visual information instead of item data.

You normally specify a layout object when creating a collection view but you can also change the layout of a collection view dynamically. The layout object is stored in the collectionViewLayout property. Setting this property directly updates the layout immediately, without animating the changes. If you want to animate the changes, you must call the setCollectionViewLayout:animated:completion: method instead.

If you want to create an interactive transition—one that is driven by a gesture recognizer or touch events—use the startInteractiveTransitionToCollectionViewLayout:completion: method to change the layout object. That method installs an intermediate layout object whose purpose is to work with your gesture recognizer or event-handling code to track the transition progress. When your event-handling code determines that the transition is finished, it calls the finishInteractiveTransition or cancelInteractiveTransition method to remove the intermediate layout object and install the intended target layout object.

Creating Cells and Supplementary Views

The collection view’s data source object provides both the content for items and the views used to present that content. When the collection view first loads its content, it asks its data source to provide a view for each visible item. To simplify the creation process for your code, the collection view requires that you always dequeue views, rather than create them explicitly in your code. There are two methods for dequeueing views. The one you use depends on which type of view has been requested:

Before you call either of these methods, you must tell the collection view how to create the corresponding view if one does not already exist. For this, you must register either a class or a nib file with the collection view. For example, when registering cells, you use the registerClass:forCellWithReuseIdentifier: or registerNib:forCellWithReuseIdentifier: method. As part of the registration process, you specify the reuse identifier that identifies the purpose of the view. This is the same string you use when dequeueing the view later.

After dequeueing the appropriate view in your delegate method, configure its content and return it to the collection view for use. After getting the layout information from the layout object, the collection view applies it to the view and displays it.

13710 questions
112
votes
3 answers

How to set UICollectionViewDelegateFlowLayout?

A UIViewController maintains a reference to a UICollectionView. The controller should modify the built-in flow layout using the UICollectionViewDelegateFlowLayout. It's pretty easy to set the view's data source to self: MyViewController.m -…
Rich Apodaca
  • 25,799
  • 16
  • 92
  • 115
110
votes
9 answers

Long press gesture on UICollectionViewCell

I was wondering how to add a long press gesture recognizer to a (subclass of) UICollectionView. I read in the documentation that it is added by default, but I can't figure out how. What I want to do is: Long press on a cell ( I have a calendar…
Oscar Apeland
  • 5,952
  • 5
  • 39
  • 85
110
votes
31 answers

UICollectionView's cellForItemAtIndexPath is not being called

Only my second time using UICollectionView's and perhaps I have bitten off more than I can chew but nevertheless: I am implementing a UICollectionView (myCollectionView) that uses custom UICollectionViewCell's that I have subclassed. The subclassed…
IkegawaTaro
  • 3,273
  • 5
  • 19
  • 25
109
votes
22 answers

Left Align Cells in UICollectionView

I am using a UICollectionView in my project, where there are multiple cells of differing widths on a line. According…
Damien
  • 1,445
  • 2
  • 14
  • 16
109
votes
9 answers

UICollectionView auto scroll to cell at IndexPath

Before loading the collection view user sets the number of image in the array of collection view. All of the cells don't fit on the screen. I have 30 cells and only 6 on the screen. The question: How to scroll to the cell with desired image…
AlexanderZ
  • 2,020
  • 3
  • 18
  • 23
105
votes
17 answers

UICollectionView Set number of columns

I just started learning about UICollectionViews. I'm wondering if anyone knows how to specify the number of columns in a collectionview. The default is set to 3 (iPhone/portrait). I've looked at the documentation and can't seem to find a concise…
SNV7
  • 2,403
  • 5
  • 23
  • 36
103
votes
18 answers

How to center align the cells of a UICollectionView?

I am currently using UICollectionView for the user interface grid, and it works fine. However, I'd like to be enable horizontal scrolling. The grid supports 8 items per page and when the total number of items are, say 4, this is how the items should…
RVN
  • 4,047
  • 5
  • 29
  • 35
102
votes
18 answers

targetContentOffsetForProposedContentOffset:withScrollingVelocity without subclassing UICollectionViewFlowLayout

I've got a very simple collectionView in my app (just a single row of square thumbnail images). I'd like to intercept the scrolling so that the offset always leaves a full image at the left side. At the moment it scrolls to wherever and will leave…
Fogmeister
  • 70,181
  • 37
  • 189
  • 274
101
votes
8 answers

Dynamic cell width of UICollectionView depending on label width

I have a UICollectionView, that loads cells from reusable cell, which contains label. An array provides content for that label. I can resize label width depending on content width easily with sizeToFit. But I cannot make cell to fit label. Here's…
pulp
  • 1,498
  • 2
  • 13
  • 21
101
votes
20 answers

How do I know that the UICollectionView has been loaded completely?

I have to do some operation whenever UICollectionView has been loaded completely, i.e. at that time all the UICollectionView's datasource / layout methods should be called. How do I know that?? Is there any delegate method to know UICollectionView…
Jirune
  • 2,180
  • 3
  • 20
  • 19
100
votes
15 answers

When to use UICollectionView instead of UITableView?

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…
wz366
  • 2,251
  • 3
  • 21
  • 27
100
votes
6 answers

Change UICollectionViewCell size on different device orientations

I am using an UICollectionView with UICollectionViewFlowLayout. I set the size of each cell through the collectionView:layout:sizeForItemAtIndexPath: When switching from portrait to landscape, I would like to adjust the size of each cell to…
Fmessina
  • 3,491
  • 3
  • 28
  • 34
95
votes
10 answers

Avoid animation of UICollectionView after reloadItemsAtIndexPaths

UICollectionView animate items after reloadItemsAtIndexPaths is called (fade animation). Is there a way to avoid this animation? iOS 6
Marcin
  • 3,279
  • 4
  • 29
  • 47
93
votes
18 answers

UICollectionView reloadData not functioning properly in iOS 7

I've been updating my apps to run on iOS 7 which is going smoothly for the most part. I have noticed in more than one app that the reloadData method of a UICollectionViewController isn't acting quite how it used to. I'll load the…
VaporwareWolf
  • 9,573
  • 8
  • 48
  • 78
91
votes
16 answers

How to adjust height of UICollectionView to be the height of the content size of the UICollectionView?

I would like the UICollectionView (The red one) to shrink to the height of the content size in this case UICollectionViewCells(the yellow ones) because there is a lot of empty space. What I tried is to use: override func layoutSubviews() { …
charbinary
  • 1,495
  • 3
  • 13
  • 23