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
49
votes
13 answers

How to add PageControl inside UICollectionView Image Scrolling

I have UICollectionView Horizontal Image listing code. I want to add PageControl when scrolling images will shows, I added pagecontrol selector and IBOutlet but how can I integrate it between UICollecitonView? My code is below: class …
SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
49
votes
3 answers

How to hide scroll bar of UICollectionView

I m working on a UICollectionView class, and it will display the scroll bar when I scroll on the list, it's possible to hide scroll bar when scrolling?
TIANLAOBA
  • 511
  • 1
  • 4
  • 5
49
votes
4 answers

Resize UICollectionView cells after their data has been set

My UICollectionView cells contain UILabels with multiline text. I don't know the height of the cells until the text has been set on the label. -(CGSize)collectionView:(UICollectionView *)collectionView …
Shocks
  • 808
  • 1
  • 9
  • 18
49
votes
7 answers

How to add HeaderView in UICollectionView like UITableView's tableHeaderView

How can I add a header view / top view (not section header) at the top of a UICollectionView? It should act excactly as UITableView's tableHeaderView property. So it needs to sit on top of the first section header view (before the section at index…
ChrHansen
  • 1,502
  • 1
  • 14
  • 22
49
votes
4 answers

UICollectionView Decoration View

Has anyone implemented a decoration view for the iOS 6 UICollectionView? It's impossible to find any tutorial on implementing a decoration view on the web. Basically in my app I have multiple sections, and I just wanted to display a decoration view…
vtruong
  • 491
  • 1
  • 5
  • 3
48
votes
9 answers

How to implement horizontally infinite scrolling UICollectionView?

I want to implement UICollectionView that scrolls horizontally and infinitely?
48
votes
3 answers

How to change UICollectionView Scroll Direction?

I have successfully implemented a UICollectionView. Is it possible to change the scrollDirection? Can you please show how to implement it programmatically?
Entitize
  • 4,553
  • 3
  • 20
  • 28
48
votes
5 answers

Subview frame is incorrect when creating UICollectionViewCell

The problem I created a UICollectionViewController with a custom UICollectionViewCell. The custom cell contains a large and rectangular UIView (named colorView) and a UILabel (named nameLabel). When the collection is first populated with its cells…
Felipe Ferri
  • 3,488
  • 2
  • 33
  • 48
48
votes
6 answers

How can I change the scroll direction in UICollectionView?

I have a UICollectionView in my storyboard based iOS app. When the device is in the portrait orientation I'd like it to scroll vertically, and when it's in Landscaper I'd like it to scroll horizontally. In UICollectionView I can see the…
Kenny
  • 1,083
  • 2
  • 8
  • 23
48
votes
2 answers

Create UICollectionViewCell subclass with xib

I'm trying to create a UICollectionViewCell subclass with linked a xib, I have do this: I have create a new xib file and I have add a UICollectionViewCell in it, then I have create this subclass file: @interface MyCell :…
Piero
  • 9,173
  • 18
  • 90
  • 160
48
votes
10 answers

How to change background color of a whole section in UICollectionView?

In UICollectionView, I want to give the whole section a uniform background color, instead of for a single cell or for the whole collection view. I don't see any delegate method to do that, any suggestions?
lichen19853
  • 1,410
  • 2
  • 14
  • 21
47
votes
9 answers

How can I check if an indexPath is valid, thus avoiding an "attempt to scroll to invalid index path" error?

How can I check to see whether an indexPath is valid or not? I want to scroll to an indexPath, but I sometimes get an error if my UICollectionView subviews aren't finished loading.
webmagnets
  • 2,266
  • 3
  • 33
  • 60
47
votes
20 answers

UICollectionView insert cells above maintaining position (like Messages.app)

By default Collection View maintains content offset while inserting cells. On the other hand I'd like to insert cells above the currently displaying ones so that they appear above the screen top edge like Messages.app do when you load earlier…
marvin_yorke
  • 3,469
  • 4
  • 25
  • 35
47
votes
13 answers

Why does UICollectionView log an error when the cells are fullscreen?

I have a UICollectionViewController using a UICollectionViewFlowLayout where my itemSize is the size of the UICollectionView. Basically, this is a line layout of cells where each cell is fullscreen and scrolls horizontally. In my…
Wes
  • 1,686
  • 1
  • 12
  • 15
47
votes
3 answers

Add a simple UIView as header of UICollectionView

I have a UICollectionView. I would like to add a header. My header would only be a UILabel. I've : 1) selected "Section Header" as a Collection View accessory in the IB. 2) created a Collection Reusable View in the IB, on the side, declared as…
Nicolas Roy
  • 3,773
  • 5
  • 27
  • 42