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
61
votes
11 answers

how to get indexPath for cell which is located in the center of UICollectionView

How can i find indexPath for cell in the middle of UICollectionView? I have horizontal scrolling and only one big cell is visible (partially two other cells on the sides are visible as well). I need to delete cell located in the center (means -…
Sasha Prent
  • 1,151
  • 2
  • 10
  • 11
61
votes
6 answers

contentSize is not updated after reloadData is called on UICollectionView

Does anyone know why contentSize is not updated immediately after reloadData is called on UICollectionView? If you need to know the contentSize the best work around I've found is the following: [_collectionView reloadData]; double delayInSeconds =…
Reid Main
  • 3,394
  • 3
  • 25
  • 42
61
votes
21 answers

UICollectionView assertion error on stale data

In the course of trying to unload one batch of images from my collection view and then replace them with another batch, I run into an error where, depending on whether the original or subsequent group of images was more or less than the intended…
user1686700
  • 758
  • 1
  • 5
  • 5
61
votes
12 answers

UICollectionView's cell disappearing

What's happening Currently I have an application that uses two UICollectionViews inside a UITableView. This way I create a Pulse News look like application. My problem with this is that sometimes the 6th and 11th row disappears completely, leaving a…
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
61
votes
5 answers

UICollectionView effective drag and drop

I am currently trying to implement the UITableView reordering behavior using UICollectionView. Let's call a UItableView TV and a UICollectionView CV (to clarify the following explanation) I am basically trying to reproduce the drag&drop of the TV,…
foOg
  • 3,126
  • 3
  • 19
  • 18
61
votes
14 answers

UICollectionView Assertion failure

I m getting this error on performing insertItemsAtIndexPaths in UICollectionView Assertion failure in: -[UICollectionViewData indexPathForItemAtGlobalIndex:], /SourceCache/UIKit/UIKit-2372/UICollectionViewData.m:442 2012-09-26 18:12:34.432 ***…
jajo87
  • 1,388
  • 2
  • 11
  • 15
59
votes
6 answers

How to create a centered UICollectionView like in Spotify's Player

I am have a lot of difficulty trying to create a UICollectionView like in Spotify's Player that acts like this: The problem for me is two fold. 1) How do I center the cells so that you can see the middle cell as well as the one of the left and…
58
votes
4 answers

UICollectionView does not scroll

I have a UICollectionView set up with a UICollectionViewDataSource that currently provides six items. These are fewer than needed to fill the screen. The problem is that my collection view only scrolls when there are enough items to fill the screen…
Dario
  • 1,315
  • 1
  • 11
  • 24
57
votes
6 answers

Could not cast value of type 'UICollectionViewCell'

I use a custom CollectionViewCell in my Storyboard. When I start the app I get this message: Could not cast value of type 'UICollectionViewCell' to TestProject.CollectionViewCell.
coco
  • 4,912
  • 2
  • 25
  • 33
57
votes
9 answers

UICollectionView autosize height

How do I properly resize a UICollectionView so that it fully displays its contents? I have tried many things, including setting its frame, calling reloadData and invalidating the layout: self.collectionView.contentSize = CGSizeMake(300,…
57
votes
2 answers

When should I set layer.shouldRasterize to YES

I've seen fixes for some lagyness issues by setting the layer property of the view view.layer.shouldRasterize = YES; I saw a great difference in performance when using a UICollectionView and preparing the cells and setting the propery. Not sure…
Avba
  • 14,822
  • 20
  • 92
  • 192
57
votes
7 answers

Getting the screen location of a cell from a UICollectionView

This isn't so much a question as an explanation of how to solve this problem. The first thing to realize is that the UICollectionView does inherit from a UIScrollView - so doing a standard lookup with a scroll view's content is the best…
Joe Jupin
  • 571
  • 1
  • 4
  • 3
57
votes
16 answers

UICollectionView align logic missing in horizontal paging scrollview

I've got a UICollectionView, which works ok, until I start scrolling. Here some pics first: As you can see it's great. As I start scrolling (paging enabled) the first one goes a bit offscreen: This is the problem. Originaly my view have 3 views…
Devfly
  • 2,495
  • 5
  • 38
  • 56
56
votes
4 answers

Why on Xcode 11, UICollectionViewCell changes size as soon as you scroll (I already set size in sizeForItem AtIndexPath:)?

I have collectionview inside tableview cell and I use nib for my collection view cell (in which I use autolayout for my imageview and labels and it is on Freeform mode). I'm setting cell size in tableviewcell class which is handling the delegate…
Reza.Ab
  • 1,195
  • 1
  • 11
  • 21
56
votes
3 answers

UICollectionView update a single cell

I am attempting to update a single cell inside a UICollectionView, specifically I am just trying to update an image in that specific cell detonated by cell.imageView. [self.collectionView reloadData] is not really an option because it makes the…