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.

13710 questions
4
votes
1 answer

How can I catch the gestures over UICollectionView?

I have a UICollectionView and a custom UICollectionViewCell I want to be able to catch the UICollectionView gestures as a UIGestureRecognizerDelegate, actually I want to handle some gestures collisions by using this delegate's method: -…
4
votes
4 answers

UICollectionView - order cells horizontally

So basically my problem is that my uicollectionview's cells are ordered from top to bottom, rather than from left to right. This is what it looks like - [1][4][7] [2][5][8] [3][6][9] This is what i want - [1][2][3] [4][5][6] [7][8][9] Another…
4
votes
2 answers

Why can't I get a header for a UICollectionView?

I'm attempting to create a UICollectionView of grouped images (currently using random images from internet), with each group having their own header. My problem is - I can't get the header view to display correctly. I set the header view's…
Frederick C. Lee
  • 7,609
  • 13
  • 56
  • 89
4
votes
1 answer

crashing at performBatchUpdates of collection view

i have a collection view which display two array of images with different section count's this will be toggled between two different view of one collectionview view - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { …
4
votes
1 answer

How to resize UICollectionViewCell in iPhone 6 and 6 Plus using autolayout

I'm working in a iOS project where i use collectionView using Autolayout and SizeClasses. custom UICollectionViewCell xib is used. Where i reuse custom cell in collectionView presenting two cells like below image in iPhone 5s. problem is while…
Rugmangathan
  • 2,958
  • 6
  • 31
  • 42
4
votes
2 answers

UICollectionView horizantal scrolling Photo Gallery with swift

I'm trying to make a Photo Gallery using UICollectionView with Swift. I want to display photos in full screen and swipe right to get to the next image. I have a working code for the portrait frame but as soon as I go to landscape it seems like my…
4
votes
4 answers

UICollectionView Swipe

I want to detect when a user swipes left or right in a collectionView in which one cell occupies the entire screen width. Is it possible to do without adding gesture recognizer. I have tried adding gesture recogniser, but it only works when we set…
user694688
  • 593
  • 1
  • 15
  • 32
4
votes
1 answer

Spacing Between Cells in CollectionView

I am using a collection view with custom cells in my xib. All the cells are exactly 45 x 45. The image that fills the view is also exactly 45 x 45 in the cell. All the spacing and insets are set to 0 in my xib and also with the code below. My…
Nicholas Gibson
  • 306
  • 1
  • 3
  • 11
4
votes
0 answers

Size methods don't get called for detail view controller in UISplitViewController iOS8

I have a UIViewController containing a UICollectionView using a UICollectionViewFlowLayout. The view controller is the detail view controller of a UISplitViewController. The view controller is the flow layout's delegate and the sizes of the…
Avner Barr
  • 13,049
  • 14
  • 82
  • 152
4
votes
2 answers

How to get Storyboard to support dynamically sized cells in UICollectionView?

I have the following layout in my dynamically-sized Storyboard: However, when running the application (depending on the orientation), cells look like the following: This is to be expected, because the previous cells had static heights and…
4
votes
1 answer

Poor scroll performance with UICollectionView - Profiling points to accessibility call

In our app, we use a customized UIViewController managing a UICollectionView for loading a stream of messages. However, we noticed poor scroll performance. On time profiling, we noticed that [UICollectionView dequeReusableCell..] call takes over 35%…
Hari
  • 401
  • 4
  • 10
4
votes
0 answers

UICollectionView Horizontal Sticky Headers

I'm implementing a UICollectionView with a set of sections and I scroll the items horizontally. I was to accomplished this but I having a hard time adding a Section header on top of the items. I tried to add a Section Header accessory but it was…
4
votes
1 answer

How to position UICollectionViewCell at bottom of screen?

There are one to three UICollectionViewCells in a UICollectionView. Is there a way to always position the cell(s) at the bottom of screen after reloadData? +----------------+ +----------------+ +----------------+ | | | …
4
votes
1 answer

How to disable gesture swipe for UICollectionView

Currently, I'm making paging UICollectionView. I want to disable gesture swipe effect from UICollectionView as I'm going to add UIPagerControl to animate the paging UICollectionView. I don't want user uses their finger to move to next screen instead…
AndroidBeginner
  • 123
  • 2
  • 12
4
votes
1 answer

UICollectionView cells covering up the scroll indicator

The sections in my UICollectionView have 0 insets on the right, making my cells flush with the right sides of the collection view. The scroll view indicator is being covered by the cells. Is there a way to make the scroll view indicator always on…
ryanwiesjahn
  • 263
  • 1
  • 11