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
0 answers

How to stop unwanted scroll in collection view after invalidating layout

i am working on collection view with dynamic cell height. What happens when i invalidate the layout the some cell grows or shrinks in height that result unwanted offset for collection view. Currently i am saving the visible top index path before…
Revinder
  • 281
  • 2
  • 13
4
votes
2 answers

SelectedBackgroundView of UICollectionViewCell visible when it should not be

I've got a UICollectionView. With some cells inside with a white background color. I've set the selectedBackgroundView to a basic purple view. My CollectionView has a constraint with a height of 0 and when I hit a button I update the constraint to …
Loadex
  • 1,392
  • 1
  • 11
  • 25
4
votes
1 answer

UICollectionReusableView height based on UILabel text

I have a UICollectionReusableView which I would like to show in the header of my collectionView. I created a XIB file for the header and and dragged a UICollectionReusableView and laid out the elements inside that using auto layout. 2 of the labels…
4
votes
1 answer

Error in sizing UICollectionViewCell in Swift

I am trying to adjust the size of each collection view cell according to the length of the label text contained within func collectionView(collectionView: UICollectionView, layout collectionViewLayout:UICollectionViewLayout, sizeForItemAtIndexPath…
barrt051
  • 222
  • 3
  • 12
4
votes
0 answers

useLayoutToLayoutNavigationTransitions and Auto Layout

I'm trying to implement a layout transition between two collection view controllers using the iOS 7+ property useLayoutToLayoutNavigationTransitions. I've a cell built in IB that is using auto layout. The two collection view controllers share the…
Luca Torella
  • 7,094
  • 4
  • 35
  • 47
4
votes
0 answers

UICollectionViewFlowLayout Subclass causes some cells to not appear

I have a vertically scrolling UICollectionView that uses a subclass of UICollectionViewFlowLayout to try and eliminate inter-item spacing. This would result in something that looks similar to a UITableView, but I need the CollectionView for other…
4
votes
1 answer

UICollectionViewCell to full screen transition

Is it possible to create a segue (or some sort of transition animation) for tapping a UICollectionView cell to make it expand to be full screen? Even expanding only the UIImageView within the cell and then swapping it out with a segue would work for…
4
votes
2 answers

How to center the first and the last cell of a horizontal UICollectionView?

I'm trying to center the first and the last cell of a horizontal collection view. The only approach i found until now was adding extra cells at the begin and the end of my UICollectionView. This method was totally a bad idea for obvious reasons but…
klefevre
  • 8,134
  • 7
  • 37
  • 68
4
votes
1 answer

UICollectionView section header crashing iOS 8

I have a UIView subclass that has a dynamically created UICollectionView. Everything works fine in ios7 with displaying the headers just fine. iOS 8 doesn't call this method at all, and the app crashes. - (UICollectionReusableView…
4
votes
1 answer

Why does UITableViewCell have a contentView property?

What's the point of this contentView property? I mean: Why aren't all the subviews just added to self? Let me get that right: Every cell is a view (UITabvleViewCell is a UIView subclass). And this fat view has another fat view with same bounds…
dontWatchMyProfile
  • 42,456
  • 49
  • 169
  • 255
4
votes
2 answers

Nothing happens if press on transparent part of uicollectionviewcell in iOS8 Today Extension

I have widget with collection of icons and these icons are on transparent background. UICollectionViewCell is also transparent. The problem is that if press on transparent part of the cell - (void)collectionView:didSelectItemAtIndexPath: is not…
4
votes
2 answers

Constraints not updated in UICollectionViewCell subclass on iOS7

I have a collection view based layout with different cell sizes depending on the content. A regular cell is 200x200 px, but if there is no content I display a cell with the same size as the collection view itself. I use -…
Johan Nordberg
  • 3,329
  • 3
  • 26
  • 52
4
votes
2 answers

UICollectionViewCell does not autosize subviews at first appearance

With XCode 6 and iOS 8 I encountered the problem that UICollectionViewCells does not autolayout / autoresize the subviews at first appearance. Once they have been shown and the cell is reused everything is fine.
Dominic Sander
  • 2,684
  • 1
  • 15
  • 29
4
votes
1 answer

iOS8 Xcode6 UICollectionView sizeForIndexPath: does not get called on rotation

My project has a UICollectionView with a custom cell that used to work perfectly on iPhone and iPad before iOS8. Now in iPad on first load cells look perfect but on rotation - (CGSize)collectionView:(UICollectionView *)collectionView…
4
votes
2 answers

Swift - UICollectionView repeats (duplicate) cells

Hi I have an array with 100 pictures url's taken from flickr. When I use UICollectionView I display 100 cells, only 8 cells are on the screen and when I scroll down to see next 8 they are the same like the previous and when I execute func…
Bogdan Bogdanov
  • 902
  • 8
  • 34
  • 69
1 2 3
99
100