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
222
votes
16 answers

UICollectionView Self Sizing Cells with Auto Layout

I'm trying to get self sizing UICollectionViewCells working with Auto Layout, but I can't seem to get the cells to size themselves to the content. I'm having trouble understanding how the cell's size is updated from the contents of what's inside the…
rawbee
  • 2,756
  • 3
  • 13
  • 21
206
votes
25 answers

Cell spacing in UICollectionView

How do I set cell spacing in a section of UICollectionView? I know there is a property minimumInteritemSpacing I have set it to 5.0 still the spacing is not appearing 5.0. I have implemented the flowout delegate method. -…
Vishal Singh
  • 4,198
  • 4
  • 22
  • 39
197
votes
5 answers

How to make a simple collection view with Swift

I'm trying to learn how to use UICollectionView. The documentation is a little hard to understand and the tutorials that I found were either in Objective C or long complicated projects. When I was learning how to use UITableView, We ❤ Swift's How to…
Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198
188
votes
15 answers

UICollectionView spacing margins

I have a UICollectionView which shows photos. I have created the collectionview using UICollectionViewFlowLayout. It works good but I would like to have spacing on margins. Is it possible to do that using UICollectionViewFlowLayout or must I…
Mert
  • 5,867
  • 3
  • 19
  • 32
185
votes
12 answers

Creating a UICollectionView programmatically

I'm looking for a guide or tutorial that will show me how to set up a simple UICollectionView using only code. I'm wading through the documentation on Apples site, and I'm using the reference manual as well. But I would really benefit from a simple…
Jimmery
  • 8,925
  • 23
  • 75
  • 142
157
votes
17 answers

UICollectionView, full width cells, allow autolayout dynamic height?

In a vertical UICollectionView , Is it possible to have full-width cells, but, allow the dynamic height to be controlled by autolayout? This strikes me as perhaps the "most important question in iOS with no really good answer." Important: Note that…
Fattie
  • 30,632
  • 54
  • 336
  • 607
146
votes
7 answers

UIRefreshControl on UICollectionView only works if the collection fills the height of the container

I'm trying to add a UIRefreshControl to a UICollectionView, but the problem is that the refresh control does not appear unless the collection view fills up the height of its parent container. In other words, unless the collection view is long enough…
Merott
  • 6,527
  • 6
  • 32
  • 50
145
votes
10 answers

How to set cell spacing and UICollectionView - UICollectionViewFlowLayout size ratio?

I'm trying to add UICollectionView to ViewController, and I need to have 3 cells 'per row' without blank space between cells (it should look like a grid). Cell width should be one third of screen size, so I thought that the layout.item width should…
135
votes
16 answers

UICollectionView inside a UITableViewCell -- dynamic height?

One of our application screens requires us to place a UICollectionView inside of a UITableViewCell. This UICollectionView will have a dynamic number of items, resulting in a height which must be calculated dynamically as well. However, I am…
Shadowman
  • 9,072
  • 17
  • 84
  • 170
129
votes
13 answers

Fatal error: unexpectedly found nil while unwrapping an Optional values

I was using an UICollectionView in Swift but I get when I try to change the text of the cell's label. func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int { return 5 } func…
rulilg
  • 1,564
  • 4
  • 14
  • 19
128
votes
21 answers

How to set UICollectionViewCell Width and Height programmatically

I am trying to implement a CollectionView. When I am using Autolayout, my cells won't change the size, but their alignment. Now I would rather want to change their sizes to e.g. //var size = CGSize(width: self.view.frame.width/10, height:…
JVS
  • 1,994
  • 3
  • 16
  • 28
125
votes
16 answers

UICollectionView current visible cell index

I am using UICollectionView first time in my iPad application. I have set UICollectionView such that its size and cell size is same, means only once cell is displayed at a time. Problem: Now when user scroll UICollectionView I need to know which…
Irfan DANISH
  • 7,467
  • 10
  • 35
  • 63
124
votes
13 answers

How to determine height of UICollectionView with FlowLayout

I've got an UICollectionView with an UICollectionViewFlowLayout, and i want to calculate its content size (for return in intrinsicContentSize needed for adjusting its height via AutoLayout). The problems is: Even if I have a fixed and equal height…
Ignatius Tremor
  • 6,476
  • 4
  • 20
  • 25
121
votes
23 answers

Paging UICollectionView by cells, not screen

I have UICollectionView with horizontal scrolling and there are always 2 cells side-by-side per the entire screen. I need the scrolling to stop at the begining of a cell. With paging enabled, the collection view scrolls the whole page, which is 2…
Martin Koles
  • 4,937
  • 8
  • 33
  • 56
115
votes
5 answers

Specifying one Dimension of Cells in UICollectionView using Auto Layout

In iOS 8 the UICollectionViewFlowLayout supports automatically resizing cells based on their own content size. This resizes the cells in both width and height according to their content. Is it possible to specify a fixed value for the width (or…
Anthony Mattox
  • 6,809
  • 5
  • 40
  • 56
1
2 3
99 100