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

How to set rounded corner for Uiimageview in Collectionview cell

I am trying to make a UICollectionviewCell as per the following design I have set corner radius for the cell, but when data is loaded on the collectonview the imageview is not getting rounded corners. The output that i am getting is as…
4
votes
4 answers

How do I get the last indexPath of a uicollectionview (NOT TABLE VIEW) ? Need it do server side paging

I have looked for ways of getting the last indexPath of a UICollectionView, although below code works for a UITableView (having one section): [NSIndexPath indexPathForRow:[yourArray count]-1 inSection:0] but not been able to achieve the same thing…
Soni A
  • 43
  • 1
  • 4
4
votes
0 answers

Collectionview Flow layout with multiple size cells

I'm trying to accomplish a collectionview with different sized cells. I've seen a few 3rd party frameworks but wanted to see if I can go it alone. Here is what I am currently have, I'm just setting every 5th cell to be bigger. How can I "add"…
tahoecoop
  • 378
  • 2
  • 9
  • 30
4
votes
1 answer

Continuously center UICollectionView cells both vertically and horizontally whenever new ones appear

The Setup: I currently have a UICollectionView that is a bunch of circles for cells to mimic the UI of the Apple Watch home screen. I made it by following this guide. In case you didn't read the article or it goes down for some reason, it's…
JoeVictor
  • 1,806
  • 1
  • 17
  • 38
4
votes
2 answers

Keep size of a custom cell in a UICollectionView when moving

I have a horizontal collection view and every cell has it's own width. I use the default UICollectionViewFlowLayout. When rearranging, cell inherits the width of the cell which it overlays during this movement, what I don't want. Any idea, how to…
Alexey Chekanov
  • 1,047
  • 14
  • 25
4
votes
1 answer

different types of cells in one collectionView

I have 4 different collectionViews in one controller, and in the first collectionView I want to have 3 different cells display. In the below code the app does not crash, but in the first indexPath.item of 0 only case 0: ("cellId") loads. It does not…
4
votes
0 answers

Why does UICollectionViewFlowLayout get called multiple times when initializing a class with a UICollectionView, and why not draw correctly?

I am trying to develop a UICollectionViewFlowLayout subclass that lays out items like a UITableView. The items use auto layout, and I also support supplementary header views. open override func layoutAttributesForSupplementaryView(ofKind…
GoldenJoe
  • 7,874
  • 7
  • 53
  • 92
4
votes
4 answers

Add UICollectionView in UICollectionViewCell

I am using Swift to build an iOS application for the Hospital I work at. Somehow, in a specific feature I have to put a UICollectionView inside the UICollectionViewCell. The one I want to achieve was for every content of the parent UICollectionView…
christ2702
  • 473
  • 6
  • 12
  • 26
4
votes
3 answers

Make uiCollectionView scrollable when not full

I want my CollectionView to still be able to scroll a bit up and down when there’s only a few cells (to get that bounce effect). In a previous question it was suggested that using self.collectionview?.alwaysBounceVerticle = true would work (this…
Nolan Ranolin
  • 409
  • 3
  • 16
4
votes
1 answer

Stretchy Header for UICollectionView

I am trying to create a stretchy header using a collectionview section header. There are a bunch of different ways to do this but I am just looking for the simplest and most straight forward method with clear insturctions on what to do. Has anybody…
Nevin Jethmalani
  • 2,726
  • 4
  • 23
  • 58
4
votes
2 answers

Open view controller programatically and dont see navigation bar. Swift 3

On my story board I have a collection view controller embedded in a navigation controller. Now from the cell of the collection view, I programaticly open the second view controller (called TwoPicsViewController), like this: let storyBoard :…
4
votes
2 answers

iOS Swift: How to recreate a Photos App UICollectionView Layout

I am wondering for quiet a while now how to create the iOS Photos App layout. How can I make it so it looks like zooming in to a collection while at the same time the navigation bar shows a back button? Is it a new view controller which gets pushed…
xxtesaxx
  • 6,175
  • 2
  • 31
  • 50
4
votes
3 answers

How can I change the properties of a cell that is not yet on the screen?

I've an app that consists of a timetable (UITableView) and a daybar (UICollectionView). The user can change their selected day by one of two methods: Select a day from the DayBar Click the "JumpToTomorrow" Button at the bottom of the table…
Declan McKenna
  • 4,321
  • 6
  • 54
  • 72
4
votes
1 answer

UICollectionView cells recalculating sizes (flicker) while scrolling

After initial table load, everything is working correctly. But after inserting a new cell, and scrolling fast up, you can see some cells recalculating their sizes (animating). It's really odd and happens to 2-3 cells max. I'm using a bottom-up…
HelloimDarius
  • 695
  • 5
  • 23
4
votes
2 answers

UICollectionView cellForItem not called

I am implementing a UICollectionView (collectionView) in a basic ViewController my collection view must retrieve 5 cells as in numbersofItemInSection code. CollectionView is showed and numbersofItemInSection function is called but cellForItemAt…
zorobabel
  • 98
  • 6
1 2 3
99
100