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
66
votes
9 answers

UICollectionView: Animate cell size change on selection

What I want to do is change the size of an UICollectionViewCell, and to animate that change, when the cell is selected. I already managed to do that unanimated by marking a cell as selected in collectionView: didSelectItemAtIndexPath: and then…
Ignatius Tremor
  • 6,476
  • 4
  • 20
  • 25
66
votes
5 answers

How to get the rect of a UICollectionViewCell?

UITableView has the method rectForRowAtIndexPath:, but this does not exist in UICollectionView. I'm looking for a nice clean way to grab a cell's bounding rectangle, perhaps one that I could add as a category on UICollectionView.
akaru
  • 6,021
  • 8
  • 58
  • 99
65
votes
6 answers

UICollectionView: how to detect when scrolling has stopped

I'm using a UICollectionView to scroll through a set of thumbnails quickly. Once scrolling ends, I'd like to display a larger hi-res version of the current thumbnail. How can I detect when the user has completed scrolling? I do implement…
George Armhold
  • 29,784
  • 45
  • 147
  • 224
64
votes
10 answers

UICollectionView - dynamic cell height?

I need to display a bunch of collectionViewCells that have different heights. the views are too complex and I don't want to manually calculate the expected height. I want to enforce auto-layout to calculate cell height Calling…
aryaxt
  • 69,636
  • 87
  • 281
  • 421
64
votes
9 answers

iOS UICollectionView prototype cell size property ignored

I'm using a UICollectionView with two prototype cells. The prototype cells have different widths and contain different controls (image view and web view). I'm definitely returning the correct prototype cell for a given index (all the cells display…
minerat
  • 1,011
  • 1
  • 7
  • 12
63
votes
9 answers

How to change UICollectionViewCell size programmatically in Swift?

I have a UICollectionView with a segmented control to switch between data. But how do I change the UICollectionViewCell size properties programmatically ? then I can custom each style appropriately for each data type. For example change the width…
RileyDev
  • 3,110
  • 3
  • 18
  • 49
63
votes
18 answers

cellForItemAtIndexPath not called but numberOfItemsInSection does

This could be a duplicate post but I haven't found the solution. I'm setting my UICollectionView as: UINib *nib = [UINib nibWithNibName:@"CollectionViewCell" bundle:[NSBundle mainBundle]]; [_collectionView registerNib: nib…
Rafael Ruiz Muñoz
  • 4,769
  • 6
  • 41
  • 79
63
votes
14 answers

Aligning right to left on UICollectionView

this is a pretty straightforward question, but I haven't been able to find a definitive answer to it on SO (if I missed it, please correct me). Basically, my question is: Is it possible to align UICollectionView row contents from right to left…
63
votes
15 answers

UICollectionView Layout Issue

I am using UICollectionView using the flow layout. I have made a custom UICollectionViewCell for the same. But on running the project the console keeps on throwing this error- The behavior of the UICollectionViewFlowLayout is not defined because: …
Nitesh
  • 1,359
  • 1
  • 15
  • 24
61
votes
10 answers

How to make both header and footer in collection view with swift

How to make both header and footer in collection view in swift ? I'm trying to combine a header and a footer together but it keep crashing, I couldn't find swift tutorial to understand it. I don't how to return supplementary view for both rather…
marrioa
  • 1,185
  • 4
  • 11
  • 28
61
votes
21 answers

UICollectionView assertion error on stale data

In the course of trying to unload one batch of images from my collection view and then replace them with another batch, I run into an error where, depending on whether the original or subsequent group of images was more or less than the intended…
user1686700
  • 758
  • 1
  • 5
  • 5
61
votes
14 answers

UICollectionView Assertion failure

I m getting this error on performing insertItemsAtIndexPaths in UICollectionView Assertion failure in: -[UICollectionViewData indexPathForItemAtGlobalIndex:], /SourceCache/UIKit/UIKit-2372/UICollectionViewData.m:442 2012-09-26 18:12:34.432 ***…
jajo87
  • 1,378
  • 2
  • 11
  • 15
60
votes
9 answers

UICollectionView - resizing cells on device rotate - Swift

I've created a UICollectionView, so that I can arrange views into neat columns. I'd like there to be a single column on devices > 500 pixels wide. In order to achieve this, I created this function: func collectionView(collectionView:…
Ben
  • 3,981
  • 4
  • 28
  • 49
60
votes
5 answers

UICollectionView effective drag and drop

I am currently trying to implement the UITableView reordering behavior using UICollectionView. Let's call a UItableView TV and a UICollectionView CV (to clarify the following explanation) I am basically trying to reproduce the drag&drop of the TV,…
foOg
  • 3,076
  • 3
  • 17
  • 18
58
votes
5 answers

contentSize is not updated after reloadData is called on UICollectionView

Does anyone know why contentSize is not updated immediately after reloadData is called on UICollectionView? If you need to know the contentSize the best work around I've found is the following: [_collectionView reloadData]; double delayInSeconds =…
Reid Main
  • 3,167
  • 3
  • 23
  • 41