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
1 answer

Most efficient way to fetch data from Firebase

I am fetching the current user's friends and storing the users in an array which gets displayed in a collection view. The friends collection view is on my home view controller which means it always remains in the users stack. My problem is, if the…
4
votes
1 answer

How to set each UICollectionView section in an individual column

I have a UICollectionView which contains a given amount of sections that each one of them consists of a given amount of rows. Now, since I assigned the grid layout as vertical, the UICollectionView looks something like this: However when the…
Hudi Ilfeld
  • 1,905
  • 2
  • 16
  • 25
4
votes
1 answer

UICollectionView repeating and showing items out of order

I have build a custom renderer for UICollectionView. I have been having one issue and I ran out of ideas of possible fixes, here it is. Whenever the user scrolls the UICollectionView, the next items to be displayed in the screen are shown out of…
4
votes
2 answers

UICollectionView items order not reversed in right to left languages

I noticed a big issue where in right to left languages, the cells order is not properly reversed, only the alignment is correct. But only for horizontal flow layout, and if the collection view contain different cell sizes! Yes, I know this sound…
4
votes
1 answer

Reload collectionview cells and not the section header

When trying to create collapsible UICollectionView sections, I update the number of items in the section dependent on its state. However, doing it this way, I reload the section which also reloads the section header aswell, and I get a very weird…
NojDavid
  • 89
  • 1
  • 10
4
votes
0 answers

Prioritize scrolling of subview UIScrollView nested inside parent UIScrollView

Note: Although technically both subviews I will be talking about are instances of UICollectionView, the concept I am trying to explain is based on the standard of UIScrollView. Therefore, I'll be referencing to the views in question as instances of…
Edwin Finch
  • 913
  • 3
  • 10
  • 24
4
votes
2 answers

Swift: asynchronously loading and displaying photos

I'm struggling with the problem of displaying photo gallery from iPhone to collectionView. Everything works fine if someone has 50 photos inside the gallery. The problem is when someone has thousands of photos, then the gallery is loading for 10…
Matt199
  • 264
  • 2
  • 18
4
votes
0 answers

Arbitrarily change sticky UICollectionView header's height when scrolling

I'm trying to create a complex sticky UICollectionView header: it must be resizable based on specific scroll criteria, e.g. when a user scrolls past a certain y position, the header will resize. it must automatically resize while the collection…
brandonscript
  • 68,675
  • 32
  • 163
  • 220
4
votes
1 answer

Resizing UICollectionView with Custom Layout after deleting cells

I downloaded this Pintrest-esc Custom Layout for my CollectionView from : https://www.raywenderlich.com/164608/uicollectionview-custom-layout-tutorial-pinterest-2 (modified it a little bit to do with cache code) So the layout works fine, i got it…
amirkrd
  • 203
  • 3
  • 8
4
votes
1 answer

How do I center align my collectionView cells?

I tried this solution here but it seems to only work for vertical layout. I'm trying to make it work for a horizontal layout. In my case, I always want 3 cells on top and 2 on bottom that is center aligned. Example:
4
votes
2 answers

CollectionView sizeForitemAt method not working for ios 9

I'm having weird issue and I'm not sure why this happening.As you can see this both images one is of ios 9 running ipad and another is ios11 running ipad. for ios9 running ipad cell is not resizing. I search a lot but did't get a answer. Please help…
jay patel
  • 238
  • 2
  • 14
4
votes
2 answers

How to add indexTitles in a collection view? (iOS 10+)

As the question, I'm trying to add an "index titles" in a collection view for a fast navigation of the contents of my UIColletionView. I've read that, starting from iOS 10, method like indexTitles(for:) and…
Alessandro Francucci
  • 1,528
  • 17
  • 25
4
votes
3 answers

Create a UICollectionView with a reversed flow layout

I want to create a view that resembles a chat view, where the newest message appears at the bottom of the screen. I would like the UICollectionViewCells to be 'pinned' to the bottom of the screen - the opposite of what the default flow layout does.…
Tometoyou
  • 7,792
  • 12
  • 62
  • 108
4
votes
1 answer

UICollectionView Custom Layout Middle column starting higher

I have a UICollectionView scrolling vertically right now. I would like to have it look like this =, where the middle row starts a little higher than the other two to create a cool and interesting effect. Any ideas on how to achieve this?
user6520705
  • 705
  • 3
  • 11
  • 38
4
votes
0 answers

Detecting end of UICollectionView reorder

I want to provide the option of UICollectionView reordering while locking the first n cells. In order to denote that the first n cells are "locked" (cannot be reordered) I want to change their background colors to a light gray. So far I've been able…
huddie96
  • 1,240
  • 2
  • 13
  • 26