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.

14313 questions
73
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,895
  • 3
  • 33
  • 44
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 Oleksandr
  • 25,505
  • 44
  • 151
  • 277
71
votes
7 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
  • 30,824
  • 50
  • 153
  • 232
70
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
  • 5,333
  • 6
  • 46
  • 92
69
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
  • 2,950
  • 3
  • 26
  • 61
69
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,265
  • 4
  • 14
  • 29
69
votes
14 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,502
  • 3
  • 30
  • 27
68
votes
7 answers

Set collectionView size. (sizeForItemAtIndexPath function is not working) Swift 3

I have a tableView and in every tableViewCell is a collectionView. I am trying to change the collectionView size with this code: func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout:…
0ndre_
  • 3,577
  • 6
  • 26
  • 44
66
votes
15 answers

UICollectionView Horizontal Paging not centered

I have a horizontal scrolling collectionView with each cell the size of the view. When I page through the collectionView it doesn't page by cell. The cells aren't in the center of the screen. I've tried a bunch of things to try to fix it and haven't…
mlevi
  • 1,433
  • 3
  • 18
  • 30
66
votes
10 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…
Jan Z.
  • 6,883
  • 4
  • 23
  • 27
64
votes
16 answers

UICollectionView and SwiftUI?

How to create grid of square items (for example like in iOS Photo Library) with SwiftUI? I tried this approach but it doesn't work: var body: some View { List(cellModels) { _ in Color.orange.frame(width: 100, height: 100) } } List…
Evgeny Mikhaylov
  • 1,696
  • 1
  • 19
  • 25
64
votes
10 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
  • 4,707
  • 5
  • 34
  • 55
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
16 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,389
  • 1
  • 15
  • 24
63
votes
1 answer

Create a book shelf by using UICollectionView

I'm going to create a book shelf in one of my projects. The basic requirements are as follows: similar to iBooks bookshelf supports both orientations well supports all kinds of iOS devices well (different resolutions) supports deleting and…
Bagusflyer
  • 12,675
  • 21
  • 96
  • 179