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

Collection view cell slow detect selected item in swift

I'm putting the collection view into table view cell and I made it can display on the cell but when I want to select the collection cell (to change the color or print cell number), the select function is not working, I need to tap the cell many time…
Lester
  • 701
  • 1
  • 9
  • 47
4
votes
3 answers

UICollectionView invalidateLayout doesn't fire the sizeForItem on screen orientation change

I have a UICollectionViewController that has a bunch of cells, and each cell has the screen size. Everything works as expected if I launch the app either in portrait or in landscape. The problem is when I change the screen orientation, and I call…
Ivan Cantarino
  • 3,058
  • 4
  • 34
  • 73
4
votes
1 answer

Method not fired in UICollectionView when added within a UIStackview

I have added a UICollectionVIew to a vertical Stack View. I connected both datasource and Delegate from storyboard and added the delegate/datasource from the code as well. However, the following method is not being called at all: internal func…
Illep
  • 16,375
  • 46
  • 171
  • 302
4
votes
3 answers

didSelectItemAt for UICollectionView is only calling on double touch

I'm facing a weird issue in UICollectionView using iOS11 emulator. In my project i have a UICollectionView with UIImageView as cells and I've created segue as Triggered Segues for cells by dragging it to a view controller. It was working great but…
Amir_P
  • 8,322
  • 5
  • 43
  • 92
4
votes
1 answer

collectionView:layout:sizeForItemAt not called swift 4 iOS 11

class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout { private let cellId = "cellId" override func viewDidLoad() { super.viewDidLoad() collectionView?.register(Cell.self,…
Van Du Tran
  • 6,736
  • 11
  • 45
  • 55
4
votes
1 answer

How to make AVPlayer & AVPlayerLayer scrollable (AVFoundation)

I am using a collectionView to display both images/videos. I am retrieving the content from Firebase. With the images, I can scroll. I added a scrollview and manually added 20 imageViews as Firebase do not allow bulk retrieval of images/content.…
SwiftLearner
  • 144
  • 13
4
votes
1 answer

Show Activity Indicator while data load in collectionView Swift

How would I go about showing an activity Indicator and a white background while the data in my collectionView loads? I currently have this: let activityView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge) override func viewDidAppear(_…
ayjoy
  • 155
  • 3
  • 10
4
votes
1 answer

iOS: PHAsset & Detecting DNG RAW Format

I got Apple's sample code for 'SamplePhotosApp' and in the photo album grid photo layout, trying to detect DNG RAW files (put up a badge if it's DNG). Default cellForItemAt: override func collectionView(_ collectionView: UICollectionView,…
Gizmodo
  • 3,151
  • 7
  • 45
  • 92
4
votes
1 answer

How can I get IGListKit to space items in a horizontally-scrolling UICollectionView?

I can't find a way to manage the spacing between items on a horizontally scrolling UICollectionView using IGListKit 3.1.1. I tried different approaches... Using UICollectionViewFlowLayout I set it like this: collectionLayout.minimumLineSpacing =…
Mick F
  • 7,312
  • 6
  • 51
  • 98
4
votes
0 answers

Reorder UICollectionView Section

Is there a possibility to drag and drop whole UICollectionViewSection. I have 6 sections with reusable header and 2 cells in that Section. I want to reorder the CollectionView by selecting the Header of that section. func…
eLeMenT
  • 153
  • 1
  • 9
4
votes
1 answer

Multiple Collection Views with in a viewController with different number of cells

There seems to be a similar question here but it didn't solve my problem. I created two collectionViews in my viewController as follows : let y = self.view.frame.height titleView = UIView(frame : CGRect(x: 0 , y: 50 , width:…
209135
  • 517
  • 5
  • 15
4
votes
1 answer

Call a UICollectionView Cell in sizeForItemAtIndexPath

I want to be able to call my UICollectionViewCell class in the sizeForItemAtIndexPath function. Like this: func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath:…
4
votes
2 answers

Propagate a custom event from a UICollectionViewCell

I have a custom UICollectionViewCell with a button inside it. When I tap the button, an event is fired inside that subclass. I want to then trigger an event on the UICollectionView itself, which I can handle in my view controller. Pseudo-code: class…
Extragorey
  • 1,654
  • 16
  • 30
4
votes
2 answers

How can I programmatically make a UICollectionView fill a UITableViewCell?

I'm trying to do something fairly simple, but I keep getting errors. I have a UITableView with multiple sections. Each section has a header and one UITableViewCell. Within each cell is a UICollectionView. All of these views must be created…
Josh
  • 2,547
  • 3
  • 13
  • 28
4
votes
1 answer

UICollectionView Crash on reloadData

UICollectionview is loaded with simple cells and there is a simple int variable for calculating the number of items in the section. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return…
Siddharthan Asokan
  • 4,321
  • 11
  • 44
  • 80
1 2 3
99
100