iOS Guides | Samples

MonoTouch.UIKit.UICollectionViewDataSource Class

The data source for a UICollectionView.

See Also: UICollectionViewDataSource


[MonoTouch.Foundation.Register("UICollectionViewDataSource", false)]
[MonoTouch.ObjCRuntime.Introduced(MonoTouch.ObjCRuntime.PlatformName.iOS, 6, 0, MonoTouch.ObjCRuntime.PlatformArchitecture.None, null)]
public abstract class UICollectionViewDataSource : NSObject, IUICollectionViewDataSource, IDisposable

See Also


As with other parts of iOS, such as UITableView and MKMapView, UICollectionView gets its data from a data source, which is exposed in MonoTouch via the UICollectionViewDataSource class. This class is responsible for providing the content to the UICollectionView including:

The UICollectionView maintains a reuse queue, which efficiently reuses UICollectionReusableViews without allocating and de-allocating any more than necessary. The UICollectionViewDataSource.GetCell and UICollectionViewDataSource.GetViewForSupplementaryElement are the functions that mutate the values of UICollectionReusableView component views. Because these methods may be called frequently during scrolling (particularly UICollectionViewDataSource.GetCell), the application developer should avoid unnecessary calculation in them.

The following code, taken from the "Introduction to Collection Views," shows a simple UICollectionViewDataSource implementation. There are 3 sections and a large animal array representing model data. The number of items per section is 1/3 of the total size of the animals array. The AnimalCell type is a subtype of UICollectionViewCell and is defined as having a UIImageImage property; the UICollectionViewDataSource.GetCell method sets this property in the reused animalCell from the animals data as appropriate. Similarly, the UIKit.UICollectionViewDataSource sets the Text property of a Header type defined in the project.

C# Example

          protected const int SectionCount = 3;
          public override int NumberOfSections (UICollectionView collectionView)
            return SectionCount;
          public override int GetItemsCount (UICollectionView collectionView, int section)
            return animals.Count / SectionCount;
          public override UICollectionViewCell GetCell (UICollectionView collectionView, MonoTouch.Foundation.NSIndexPath indexPath)
            var animalCell = (AnimalCell)collectionView.DequeueReusableCell (animalCellId, indexPath);
            var animal = animals [indexPath.Section * (animals.Count / SectionCount) + indexPath.Row];
            animalCell.Image = animal.Image;
            return animalCell;
          public override UICollectionReusableView GetViewForSupplementaryElement (UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
            var headerView = (Header)collectionView.DequeueReusableSupplementaryView (elementKind, headerId, indexPath);
            headerView.Text = "Supplementary View Section " + indexPath.Section.ToString ();
            return headerView;

The UICollectionViewSource combines the UICollectionViewDataSource API and the UICollectionViewDelegate API in a single convenience class. Rather than creating two classes to assign to the UICollectionView.DataSource and UICollectionView.Delegate properties, a single UICollectionViewSource can be created and assigned to the UICollectionView.Source property.

Related content


Namespace: MonoTouch.UIKit
Assembly: monotouch (in monotouch.dll)
Assembly Versions:

The members of MonoTouch.UIKit.UICollectionViewDataSource are listed below.

See Also: NSObject

Public Constructors

Default constructor, initializes a new instance of this class.
A constructor that initializes the object from the data stored in the unarchiver object.
Constructor to call on derived classes to skip initialization and merely allocate the object.
A constructor used when creating managed representations of unmanaged objects; Called by the runtime.

Public Methods

CanMoveItem(UICollectionView, NSIndexPath) : Boolean
GetCell(UICollectionView, NSIndexPath) : UICollectionViewCell
The cell at the specified indexPath.
GetItemsCount(UICollectionView, Int32) : Int32
The number of items in the specified section.
GetViewForSupplementaryElement(UICollectionView, NSString, NSIndexPath) : UICollectionReusableView
Retrieves the UICollectionReusableView for the specified elementKind at the specified indexPath.
MoveItem(UICollectionView, NSIndexPath, NSIndexPath)
NumberOfSections(UICollectionView) : Int32
The number of sections in the UICollectionView.