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

uicollectionview's dynamic height as per row values

hello friends i am using collection view in iPhone application to show list. I want to set the height of the collection view as per size of my list. how can I set it? also have the problem when I use set selected method of cell in the collection…
Premal Khetani
  • 3,167
  • 1
  • 22
  • 56
4
votes
1 answer

Strange UICollectionView behavior when paging enabled and multiple cells per page

I have a UICollectionView, horizontal scrolling, paging=YES, displaying three cells per page. In the event its datasource has four items, the collectionView will have "two pages". Strangely, upon tapping on cells, the collectionView always snaps…
opfeffer
  • 593
  • 4
  • 16
4
votes
0 answers

Dynamic UICollectionView content size height is too long

I need to create a UICollectionView cells with dynamic heights according to each cell's image view height. I have created a subclass from UICollectionViewFlowLayout to implement the desired behaviour as follows: #define numColumns 2 - (NSArray…
4
votes
2 answers

collectionViewContentSize() vs contentSize

What is the difference between collectionViewController.collectionViewLayout.collectionViewContentSize() and collectionViewController.collectionView.contentSize ? What do you prefer to use?
János
  • 27,206
  • 24
  • 130
  • 270
4
votes
3 answers

How Can I Animate the Size of A UICollectionViewCell on Scroll so the middle one is largest?

I have a UICollectionView that shows several rows with one, full-width column (looks like a UITableView) What I'd like to achieve is something similar to this: ... where the middle cell has a much greater height. As the user scrolls up and down,…
4
votes
1 answer

Chat Application using UITableView Vs UIcollectionView (iOS)?

I am going to develop a chat application. but my concern is if i should go for UITableView or UICollectionView for that bubble chat view like in whatsapp. Given below is screenshot of WhatsApp Chat. If anyone Know what WhatsApp used for this…
Gagan Joshi
  • 3,337
  • 2
  • 19
  • 30
4
votes
0 answers

Stop `UICollectionViewCell` disappearing after `UICollectionView` bounds change

I have a UICollectionView that animates its bounds when a user lifts their finger from the screen. The collection view either grows or shrinks based on how much the user scrolled. This works great if the user lifts their finger up whilst the…
4
votes
1 answer

Collection view not scrolling when interacting on a cell

My collection view controller is not responding when swiping on a cell(touch begin in a point on the cell). But it interacts when swipe began a point such as spaces between the cells. It was previously scrolling vertically, not touch gestures(there…
Ugur
  • 194
  • 14
4
votes
3 answers

UICollectionView header view not dequeueing properly

I am trying to add a header to a collection view. I am using a custom layout that scrolls horizontally, it is used to view a list of avatar images for friends. I can get the header to appear but it does NOT dequeue. As soon as the header view goes…
DBoyer
  • 3,012
  • 4
  • 20
  • 32
4
votes
3 answers

Preserving UICollectionView contentOffset when returning from a presented view controller

I have a UIViewController subclass that contains a UICollectionView. Selecting a cell presents a new view controller. When I return to the first view controller, the contentOffset of the collection view is reset to CGPointZero. From my research this…
Darren
  • 9,855
  • 18
  • 60
  • 106
4
votes
2 answers

Don't dismiss keyboard with reload collectionViewCell

I have a UICollectionView with custom cells that have a UITextView inside. The user can tap any text inside the text field. When the text field's height gets bigger than the size of the cell, the cell advises the collection view to reload…
DaSilva
  • 1,347
  • 1
  • 19
  • 42
4
votes
0 answers

UICollectionView changes content offset when rotated

I have a UICollectionViewController subclass embedded in another view controller. It seems that no matter what I do, the collection view shifts its frame upward by 128px each time I rotate the device to landscape. I've turned off "Adjust Scroll View…
Caleb
  • 120,112
  • 19
  • 171
  • 259
4
votes
6 answers

UICollectionView with dynamic cell content - get cell to remember state

I have a UICollectionView that contains dynamic content. The problem I am having is that when the cell is dequeued it forgets its state. In my - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView…
DevC
  • 6,606
  • 9
  • 40
  • 80
4
votes
2 answers

How do I achieve spacing between pages in a UICollectionView?

UICollection has paging just like a UIPageViewController. With the latter, you have UIPageViewControllerOptionInterPageSpacingKey to easily set the spacing. How is this best achieved with a UICollectionView?
Doug Smith
  • 27,683
  • 54
  • 189
  • 363
4
votes
0 answers

UICollectionView transition between layouts with different datasources

I've been wrestling with transitions between layouts, view controllers, collection views, and their animations for a while now. My goal for now is just to copy some functionality of the iOS Photos app: transitioning from a view with many stacks to a…
MDrum
  • 41
  • 3
1 2 3
99
100