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
84
votes
6 answers

UICollectionView animate data change

In my Project I use UICollectionView to display a grid of icons. The user is able to change the ordering by clicking a segmented control which calling a fetch from core data with different NSSortDescriptor. The amount of data is always the same,…
Nimrod7
  • 1,385
  • 2
  • 16
  • 30
81
votes
12 answers

UICollectionReusableView method not being called

I want my sections in the UICollectionView to have a header with an image. I have followed these steps: at the storyboard, assigned a header as an accessory for my UICollectionView gave it an identifier created a subclass of…
Eden V.
  • 833
  • 1
  • 6
  • 10
79
votes
11 answers

UICollectionView flowLayout not wrapping cells correctly

I have a UICollectionView with a FLowLayout. It will work as I expect most of the time, but every now and then one of the cells does not wrap properly. For example, the the cell that should be on in the first "column" of the third row if actually…
lindon fox
  • 3,222
  • 3
  • 29
  • 54
77
votes
14 answers

Swift: How to refresh UICollectionView layout after rotation of the device

I used UICollectionView (flowlayout) to build a simple layout. the width for each cell is set to the width of screen using self.view.frame.width but when I rotate the device, the cells don't get updated. I have found a function, which is called…
Talha Ahmad Khan
  • 2,846
  • 3
  • 19
  • 35
76
votes
14 answers

Auto-layout: What creates constraints named UIView-Encapsulated-Layout-Width & Height?

My layout constraints are fine in Interface Builder but an exception occurs at runtime thanks to some part of the framework applying fixed height and width constraints that I really don't want. Why are they there, and how to turn them off? They're…
Reuben Scratton
  • 37,934
  • 8
  • 74
  • 84
75
votes
7 answers

UICollectionView: must be initialized with a non-nil layout parameter

I added UICollectionView by code. Now, the app crash with message: UICollectionView must be initialized with a non-nil layout parameter. Do you have any idea to fix it? CollectionCell is custom class of UICollectionViewCell. @property (nonatomic,…
kusumoto_teruya
  • 1,771
  • 3
  • 15
  • 24
75
votes
9 answers

UICollectionView adds top margin

I want to put a UICollectionView control that shows thumbs horizontally (only a single line of thumbs). For some reason the UICollectionView push the thumbs 44 pixels down, so the "0" height is actually "44". I assume it might be adding this space…
bashan
  • 3,424
  • 6
  • 37
  • 57
74
votes
24 answers

Keeping the contentOffset in a UICollectionView while rotating Interface Orientation

I'm trying to handle interface orientation changes in a UICollectionViewController. What I'm trying to achieve is, that I want to have the same contentOffset after an interface rotation. Meaning, that it should be changed corresponding to the ratio…
72
votes
16 answers

Snap to center of a cell when scrolling UICollectionView horizontally

I know some people have asked this question before but they were all about UITableViews or UIScrollViews and I couldn't get the accepted solution to work for me. What I would like is the snapping effect when scrolling through my UICollectionView…
Mark Bourke
  • 9,052
  • 7
  • 21
  • 29
71
votes
18 answers

iOS 10 bug: UICollectionView received layout attributes for a cell with an index path that does not exist

Running my app in a device with iOS 10 I get this error: UICollectionView received layout attributes for a cell with an index path that does not exist In iOS 8 and 9 works fine. I have been researching and I have found that is something related to…
cmacera
  • 1,099
  • 1
  • 10
  • 21
71
votes
3 answers

UICollectionView adding UICollectionCell

When I try to put UICollectionCell to UICollectionView in Interface Builder I can't put it with unknown reasons. The cell is going to the tools bar without adding to UICollectionView I am using: iOS SDK 6.0 XCode 4.5.1 I don't use Storyboard
Matrosov Alexander
  • 20,713
  • 42
  • 130
  • 259
70
votes
21 answers

the behavior of the UICollectionViewFlowLayout is not defined, because the cell width is greater than collectionView width

2015-08-18 16:07:51.523 Example[16070:269647] the behavior of the UICollectionViewFlowLayout is not defined because: 2015-08-18 16:07:51.523 Example[16070:269647] the item width must be less than the width of the UICollectionView minus the…
70
votes
10 answers

Dynamically setting layout on UICollectionView causes inexplicable contentOffset change

According to Apple's documentation (and touted at WWDC 2012), it is possible to set the layout on UICollectionView dynamically and even animate the changes: You normally specify a layout object when creating a collection view but you can also…
Timothy Moose
  • 9,773
  • 3
  • 31
  • 44
68
votes
13 answers

How to make Supplementary View float in UICollectionView as Section Headers do in UITableView plain style

I'm struggling to achieve a "floating section header" effect with UICollectionView. Something that's been easy enough in UITableView (default behavior for UITableViewStylePlain) seems impossible in UICollectionView without lots of hard work. Am I…
100grams
  • 3,314
  • 3
  • 27
  • 25
67
votes
12 answers

UICollectionView - Horizontal scroll, horizontal layout?

I have a UIScrollView that lays out a grid of icons. If you were to imagine the layout for the iOS Springboard, you'd be pretty close to correct. It has a horizontal, paged scroll (just like Springboard). However, it appears that the layout is…
Shadowman
  • 9,072
  • 17
  • 84
  • 170