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

Animation Duration for UICollectionView selectItemAtIndexPath:animated:scrollPosition:

I have tried to set the duration (with the normal methods that I would try) for setting the animation duration for UICollectionView's selectItemAtIndexPath:animated:scrollPosition: method (or the scrollToItemAtIndexPath:atScrollPosition:animated:…
dtuckernet
  • 7,817
  • 5
  • 39
  • 54
36
votes
6 answers

UICollectionView with a sticky header

I found a blog on how to make sticky headers and it works great. Only thing is I don't think it takes into account the sectionInserts. This is how its intended to look: I have my inserts: collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(16,…
Padin215
  • 7,444
  • 13
  • 65
  • 103
35
votes
5 answers

Grid layout with CollectionView in Swift

I would like to achieve this result: Searching around I found out that probably the way to do it is using UICollectionView, so no problem with that since there are many tutorials and questions on Stack Overflow. I have 3 questions: I cannot find…
Mat
  • 6,236
  • 9
  • 42
  • 55
35
votes
6 answers

UICollectionView is scrolling when selecting a cell

I'm having trouble when i try to select an item inside a UICollectionView since when I click on it it scrolls a little bit. I know didSelectItemAtIndexPath is being called but I want to prevent the scrolling when selecting. I only want the…
Miguelme
  • 609
  • 1
  • 6
  • 17
35
votes
5 answers

UICollectionView iOS 9 issue on project with RTL languages support

It seems like Apple's new feature of auto-flip interface on RTL languages cause problems when using UICollectionView. I used constraints of type Trailing/Leading for the collection view and they switched their values, as they should, on RTL…
MatanGold
  • 739
  • 10
  • 18
35
votes
10 answers

What's causing this iOS crash? UICollectionView received layout attributes for a cell with an index path that does not exist

I'm working on an app that has a UICollectionViewController that is crashing in certain mysterious situations that are hard to reproduce. The log for the crash looks like this: *** Assertion failure in -[UICollectionViewData validateLayoutInRect:],…
Drew
  • 926
  • 1
  • 9
  • 13
35
votes
9 answers

UICollectionView estimatedItemSize - last cell is not aligned

I want to make a usual horizontalScrolling flowLayout UICollectionView with estimatedItemSize and preferredLayoutAttributesFittingAttributes in cell. But there is something wrong with last cell. Any idea where is the issue? Project…
artysx
  • 533
  • 5
  • 14
35
votes
7 answers

Where to highlight UICollectionViewCell: delegate or cell?

According to the Collection View Programming Guide one should handle the visual state of the cell highlights in the UICollectionViewDelegate. Like this: - (void)collectionView:(PSUICollectionView *)collectionView…
hpique
  • 119,096
  • 131
  • 338
  • 476
35
votes
2 answers

UICollectionView for Springboard like folders

I am trying to achieve the following effect: A UICollectionView displays a grid of cells for a parent type of object, e. g. a photo album. When I tap one of these items, I would like to scroll that element to the top of the screen and open a…
Daniel Schneller
  • 13,728
  • 5
  • 43
  • 72
35
votes
9 answers

UICollectionView automatically scroll to bottom when screen loads

I'm trying to figure out how to scroll all the way to the bottom of a UICollectionView when the screen first loads. I'm able to scroll to the bottom when the status bar is touched, but I'd like to be able to do that automatically when the view loads…
SNV7
  • 2,563
  • 5
  • 25
  • 37
35
votes
9 answers

How are cells deselected programmatically in UICollectionView when allowMultipleSelection is enabled?

I have allowMultipleSelection enabled in a collection view. The cells change to and from their selected states when tapped. All good. However, when I want to reset the entire view to selected state:NO using the code below, the cells appears to be…
Steve
  • 988
  • 1
  • 12
  • 25
34
votes
5 answers

Allow A Header View for Only Certain Sections Using an iOS UICollectionView

The code below displays my header view correctly, but for each of the sections in the UICollectionView: -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind …
34
votes
2 answers

UICollection View Adjust Cell Size for Screen Size

Currently i have a collection view which takes up most of my screen at the 3.5 inch screen size.The collection view is 51 pixels from the top and 20 pixels from the bottom of the screen. I am using the cells with UIPageControl to create a paged…
34
votes
5 answers

UICollectionViewCell Border / Shadow

When building an iPad App, how can you draw a border around a UICollectionViewCell? More details: I implemented a class ProductCell which extends UICollectionViewCell. Now, I would like to assign some fancy details, e.g. a border, shadow, etc.…
itsame69
  • 1,540
  • 5
  • 17
  • 37
33
votes
4 answers

Collection View Compositional Layout with estimated height not working

I want my app to be optimized for every accessibility options including the text size. I made a collectionView layout based on sections with a compositional layout. So I need my cell's height to grow with it's content. I thought using…
Que20
  • 1,469
  • 2
  • 13
  • 27