UIKit.UICollectionViewDataSource Class
The data source for a UIKit.UICollectionView.

See Also: UICollectionViewDataSource Members

Syntax

[Foundation.Model]
[Foundation.Protocol]
[Foundation.Register("UICollectionViewDataSource", false)]
[ObjCRuntime.Availability(Introduced=ObjCRuntime.Platform.iOS_6_0)]
public abstract class UICollectionViewDataSource : Foundation.NSObject, IUICollectionViewDataSource, IDisposable

See Also

UICollectionViewSource
UICollectionView
UICollectionViewController
UICollectionReusableView

Remarks

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

The UIKit.UICollectionView maintains a reuse queue, which efficiently reuses 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 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 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 UIKit.UICollectionViewCell and is defined as having a UIKit.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, 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 UIKit.UICollectionViewSource combines the UIKit.UICollectionViewDataSource API and the 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 UIKit.UICollectionViewSource can be created and assigned to the UICollectionView.Source property.

Related content

Requirements

Namespace: UIKit
Assembly: Xamarin.iOS (in Xamarin.iOS.dll)
Assembly Versions: 0.0.0.0