See Also: UICollectionViewDataSource Members
As with other parts of iOS, such as MonoTouch.UIKit.UITableView and MonoTouch.MapKit.MKMapView, MonoTouch.UIKit.UICollectionView gets its data from a data source, which is exposed in MonoTouch via the MonoTouch.UIKit.UICollectionViewDataSource class. This class is responsible for providing the content to the MonoTouch.UIKit.UICollectionView including:
The MonoTouch.UIKit.UICollectionView maintains a reuse queue, which efficiently reuses MonoTouch.UIKit.UICollectionReusableViews without allocating and de-allocating any more than necessary. The UICollectionViewDataSource.GetCell and UICollectionViewDataSource.GetViewForSupplementaryElement are the functions that mutate the values of MonoTouch.UIKit.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 MonoTouch.UIKit.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 MonoTouch.UIKit.UICollectionViewCell and is defined as having a MonoTouch.UIKit.UIImageImage property; the UICollectionViewDataSource.GetCell method sets this property in the reused animalCell from the animals data as appropriate. Similarly, the MonoTouch.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 MonoTouch.UIKit.UICollectionViewSource combines the MonoTouch.UIKit.UICollectionViewDataSource API and the MonoTouch.UIKit.UICollectionViewDelegate API in a single convenience class. Rather than creating two classes to assign to the UICollectionView.DataSource and UICollectionView.Delegate properties, a single MonoTouch.UIKit.UICollectionViewSource can be created and assigned to the UICollectionView.Source property.