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
55
votes
4 answers

Dynamic size UICollectionView cell

1) How can i achieve as shown in image with UICollectionView? 2) I've tried -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath method &…
55
votes
15 answers

UICollectionView only calling didSelectItemAtIndexPath if user double taps, will not call when user single taps

I have a UICollectionView which is about the size of the screen. The UICollectionViewCells that it displays are the same size as the collectionView. Each cell has a UIImage which is the size of the cell. The CollectionView has paging enabled so…
Kris Gellci
  • 9,539
  • 6
  • 40
  • 47
55
votes
9 answers

Reset scroll on UICollectionView

I have a horizontal UICollectionView which works fine and scrolls. When I tap an item I update my data and call reloadData. This works and the new data is displayed in the UICollectionView. The problem is the scroll position doesn't change and it is…
MulletDevil
  • 919
  • 2
  • 8
  • 16
54
votes
2 answers

UICollectionView doesn't bounce when datasource has only 1 item

I have a UICollectionView with a custom UICollectionViewFlowLayout. It scrolls horizontally and only shows one item at a time (full height). I have pagingEnabled = YES so that it sticks to pages. The UICollectionView shows photos within a…
54
votes
11 answers

Start UICollectionView at a specific indexpath

I currently have a collection view that does horizontal paging where each cell is fullscreen. What I want to do is for the collectionview to start at a specific index when it shows. Right now I'm using…
Zack
  • 881
  • 1
  • 9
  • 13
53
votes
8 answers

In iOS 12, when does the UICollectionView layout cells, use autolayout in nib

Same code like this collectionLayout.estimatedItemSize = CGSize(width: 50, height: 25) collectionLayout.itemSize = UICollectionViewFlowLayoutAutomaticSize collectionLayout.minimumInteritemSpacing = 10 for _ in 0 ..< 1000 { let length =…
EvilHydra
  • 552
  • 1
  • 5
  • 9
53
votes
4 answers

Auto Layout in UICollectionViewCell not working

I have a simple UICollectionView with cells that have a single UITextView. The UITextView is constrained to the edges of the cell, so they should stay the same size as the cell size. The problem I'm having is that for some reason these constraints…
ryanthon
  • 2,524
  • 3
  • 17
  • 18
53
votes
10 answers

UICollectionView performance - _updateVisibleCellsNow

I'm working on a custom UICollectionViewLayout that displays cells organized by day/week/month. It is not scrolling smooth, and it looks like the lag is caused by [UICollectionView _updateVisibleCellsNow] being called on each rendering…
Oskar
  • 1,047
  • 2
  • 12
  • 19
53
votes
15 answers

UICollectionView Select and Deselect issue

So I have a main object that has many images associated with it. An Image is also an object. Say you have a collection view controller, and in that controller you have cellForItemAtIndexPath well based on the main object, if it has the current…
bworby
  • 1,045
  • 1
  • 10
  • 17
52
votes
15 answers

UICollectionView scroll to item not working with horizontal direction

I have a UICollectionView within a UIViewController with paging enabled. For some strange reason collectionView.scrollToItem works when the direction of the collectionview is vertical but doesn't when direction is horizontal. Is this there…
kye
  • 2,166
  • 3
  • 27
  • 41
52
votes
7 answers

What is a 'UIView-Encapsulated-Layout-Width' constraint?

I keep getting 'Unable to simultaneously satisfy constraints' exceptions (Xcode 5, iOS 7, both device and simulator), where one of the constraints in the list is something like this: "
51
votes
12 answers

UICollectionView cell selection and cell reuse

Upon cell selection, I want to handle changing the cell appearance. I figured the delegate method collectionView:didSelectItemAtIndexPath: & collectionView:didDeselectItemAtIndexPath: is where I should edit the…
Padin215
  • 7,444
  • 13
  • 65
  • 103
51
votes
12 answers

How do you determine spacing between cells in UICollectionView flowLayout

I have a UICollectionView with a flow layout and each cell is a square. How do I determine the spacing between each cells on each row? I can't seem to find the appropriate settings for this. I see there's a min spacing attributes on the nib file for…
adit
  • 32,574
  • 72
  • 229
  • 373
50
votes
5 answers

UICollectionView header dynamic height using Auto Layout

I have a UICollectionView with a header of type UICollectionReusableView. In it, I have a label whose length varies by user input. I'm looking for a way to have the header dynamically resize depending on the height of the label, as well as other…
joda
  • 711
  • 2
  • 8
  • 16
50
votes
5 answers

How to create UICollectionViewCell programmatically

I'm trying to create UICollectionView programatically. I need to add labels inside the cells, so I Created CollectionViewCell class. This is the class: import UIKit class MyCollectionViewCell: UICollectionViewCell { override init(frame:…
s_s
  • 709
  • 1
  • 7
  • 13