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

iOS 8 Swift UICollectionView reloadData() causing views (images) to jump cells

I have a UICollectionView of 20 cells with an imageView inside of them. when a user clicks on the correct cell, I draw another imageView in that cell (a correctCircle let's call it). This works fine. But I also have a header in the UICollectionView…
kakubei
  • 4,942
  • 1
  • 37
  • 61
4
votes
1 answer

Assertion failure in UICollectionView with supplementary views / section headers, dataSource is nil?

Every once in a while on of my Apps reports this assertion failure via Crashlytics: Fatal Exception: NSInternalInconsistencyException UICollectionView dataSource is not set 0 CoreFoundation __exceptionPreprocess + 126 3 Foundation …
Gereon
  • 14,827
  • 4
  • 36
  • 62
4
votes
3 answers

UICollectionView: Smooth scrolling with many visible cells

I'm trying to optimize the scrolling speed and smoothness of my UICollectionView (using a custom layout) that has about 400 cells, 150 of which are visible on the screen at the same time. The cells themselves aren't very complex: They consist of a…
Marius Schulz
  • 14,538
  • 12
  • 58
  • 94
4
votes
4 answers

Lazy loading in uicollectionview

here is the code for my collectionview it is showing records but loading really please tell me how can i implement lazy loading on this i also have a placeholder pic in my project - (UICollectionViewCell *)collectionView:(UICollectionView…
4
votes
1 answer

Implement two CollectionView Layouts in one UICollectionView

Someone already asked this here (Can we set different flowlayout in different sections within a UICollectionView?)..... but no one has responded and I have too little reputation to comment and bump the post. I'm using a collectionView to sort of…
Brie
  • 341
  • 1
  • 3
  • 13
4
votes
1 answer

Set anchor point in UICollectionViewLayoutAttributes

I want to perform transform animation in UICollectionViewLayout. Well that is achieved easily but I can't find way to set the anchorPoint of UICollectionViewLayoutAttributes. I want to perform door opening and closing animation while collection view…
Jay Gajjar
  • 2,571
  • 2
  • 18
  • 34
4
votes
2 answers

Swift: Implementing collection view without collection view controller

I set up a CollectionViewController and got things working pretty well and straight forward. class CollectionController: UICollectionViewController, UICollectionViewDelegateFlowLayout { . . . override func…
MMachinegun
  • 2,924
  • 2
  • 24
  • 41
4
votes
1 answer

Update constraints for supplementary view of UICollectionView on orientation change

I'm using a header (supplementary view) in my UICollectionView. On the header I have label which has a certain distance from the left side. I'm doing my calculation and setting the constant of a constraint. Everything does work as expected. If the…
testing
  • 17,950
  • 38
  • 208
  • 373
4
votes
0 answers

UICollectionview self sizing cells is not working

I want to use self sizing cells in my collection view. My cells don't use auto layout - I've implemented the methods as required but still they are not resizing properly. Attached is the code: @interface Cell : UICollectionViewCell @end …
Avner Barr
  • 13,049
  • 14
  • 82
  • 152
4
votes
0 answers

Resize and select taped UicollectionViewCell with animation

I want to enlarge all cells, when i tapped on any cell and scroll UICollectionView to this cell with animation. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { fullScreen =…
4
votes
2 answers

UICollectionViewController Black Screen

I am trying to show my collection view but all I get is a black screen. I know I'm missing something somewhere but I can't seem to find what it is. #import "StoreCollectionViewController.h" #import "StoreItemCell.h" @interface…
raginggoat
  • 3,450
  • 9
  • 42
  • 99
4
votes
3 answers

Unable to create bottom border on UICollectionView Cell

I need to add a 1px bottom border to my UICollectionView cells but I can't get it working, I tried the following code but the border doesn't show: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView…
LS_
  • 6,162
  • 8
  • 42
  • 82
4
votes
3 answers

pull to refresh not working with small amount of cells

I have a feed view controller (implemented with a UICollectionViewController) When i don't have enough cells to cover the hall screen height the pull to refresh isn't working. How can i fix this? Code: var refreshControl: UIRefreshControl! …
ilan
  • 4,070
  • 5
  • 31
  • 70
4
votes
0 answers

Get the actual line spacing and inter item spacing for a collection view?

When you implement a collection view, you can specify the minimumLineSpacing and minimumInteritemSpacing, which will specify the minimum amount of padding to apply between rows and items in each row respectively, and iOS will automatically increase…
Jordan H
  • 45,794
  • 29
  • 162
  • 306
4
votes
2 answers

UIViewController calls sizeForItemAtIndexPath for all cells on load

I have a UICollectionView with UICollectionViewFlowLayout. While testing with large data (5,000+ cells) I noticed a lag when loading. I found out that at loading the sizeForItemAtIndexPath is called for EVERY cell. Since I am doing a messaging like…