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
41
votes
7 answers

How do I define the size of a CollectionView on rotate

I have a viewcontroller with 2 CollectionView Controllers. I would like on rotation that only one of the collection views resize to a custom size while the other remains the same. I have tried to use: - (CGSize)collectionView:(UICollectionView…
Robert Farr
  • 680
  • 2
  • 7
  • 19
41
votes
12 answers

UICollection View Flow Layout Vertical Align

By default, when you are using the flow layout in a collection view, cells are centered vertically. Is there a way to change this alignment ?
R.Lambert
  • 1,160
  • 1
  • 11
  • 25
40
votes
7 answers

UICollectionView horizontal paging with 3 items

I need to show 3 items in a UICollectionView, with paging enabled like this but I am getting like this I have made custom flow, plus paging is enabled but not able to get what i need. How can i achieve this or which delegate should i look into,…
Raheel Sadiq
  • 9,847
  • 6
  • 42
  • 54
39
votes
11 answers

UICollectionView: current index path for page control

I'm using a UICollectionView with a flow layout to show a list of cells, I also have a page control to indicate current page, but there seems to be no way to get current index path, I know I can get visible cells: UICollectionView current visible…
hzxu
  • 5,753
  • 11
  • 60
  • 95
39
votes
8 answers

Invalid update: invalid number of items on UICollectionView

I am stumped on this. Here is my scenario. In my Appdelegate, I am creating An instance of a view controller that will be presented modally to collect two pieces of data from the user A tableView controller A navigation controller that I init…
Tommy Alexander
  • 701
  • 1
  • 7
  • 17
39
votes
4 answers

UICollectionView wrong contentSize on first load, correct after that

I have a common UICollectionView with paging and all. Still trying to figure out why on viewDidLoad:, viewWillAppear: and viewDidAppear: , only on first view call, I get the wrong size when calling myCollectionView.collectionView.contentSize.width.…
klauslanza
  • 671
  • 1
  • 6
  • 8
39
votes
15 answers

Error could not dequeue a view of kind UICollectionElementKindCell

Taking first plunge with collection views and am running into this error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell -…
Anti-Dentite
  • 551
  • 1
  • 6
  • 11
38
votes
11 answers

CollectionView background clearColor not working

I'm developing a small collectionview 'framework' to behave like a browser tab bar (think chrome) on the iPad. The code is all done, custom flow layout, reordering, and so on and is organized as so : • TabBarCollectionViewController .h/.m/.xib…
nebuto
  • 994
  • 2
  • 9
  • 16
38
votes
1 answer

Custom animation when switching from one UICollectionViewLayout to another?

As a test I made one layout that displays cells in a vertical line and another that displays them in a horizontal layout. When I call [collectionView setCollectionViewLayout:layout animated:YES]; it animates between the two positions very…
Mason Cloud
  • 1,266
  • 1
  • 15
  • 20
38
votes
21 answers

UICollectionViewFlowLayout Size Warning When Rotating Device to Landscape

We are using a UICollectionView to display cell that cover the full screen (minus the status and nav bar). The cell size is set from self.collectionView.bounds.size: - (void)viewWillAppear:(BOOL)animated { // // value isn't correct with the…
37
votes
11 answers

UICollectionView Custom Cell to fill width In Swift

I've been trying to figure-out how can i make the cell fill the width, as you can see in the picture the width between the cells is too big. i am using custom cell with only one imageView. I tried to customize it from the storyboard but i guess…
AaoIi
  • 8,288
  • 6
  • 45
  • 87
36
votes
7 answers

Warning: UICollectionViewFlowLayout has cached frame mismatch for index path 'abc'

This is the code causing the warning: private override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { let attributes = super.layoutAttributesForItemAtIndexPath(indexPath) let distance =…
Dhruv Goel
  • 2,715
  • 1
  • 17
  • 17
36
votes
8 answers

UICollectionView - didDeselectItemAtIndexPath not called if cell is selected

The first thing I do is to set the cell selected. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView…
zeiteisen
  • 7,078
  • 5
  • 50
  • 68
36
votes
5 answers

UICollectionView container padding

I am trying to add padding to the container of a UICollectionView. I would like it to appear as such that there is a 10pt padding all around. So in the example screen, there is a 10pt padding on the bottom from: layout.minimumInteritemSpacing =…
nmock
  • 1,907
  • 2
  • 20
  • 29
36
votes
6 answers

UICollectionview Scrolling choppy when loading cells

I have a gallery in my app utilizing a UICollectionView. The cells are approximately 70,70 size. I am using ALAssets from the ALAssetLibrary in the gallery which I have stored in a list. I am using the usual pattern for populating the…
Avba
  • 14,822
  • 20
  • 92
  • 192