Share via


IUICollectionViewDataSource Interface

Definition

The data source for a UICollectionView.

[Foundation.Protocol(Name="UICollectionViewDataSource", WrapperType=typeof(UIKit.UICollectionViewDataSourceWrapper))]
[Foundation.ProtocolMember(IsProperty=false, IsRequired=true, IsStatic=false, Name="GetItemsCount", ParameterByRef=new System.Boolean[] { false, false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView), typeof(System.IntPtr) }, ReturnType=typeof(System.IntPtr), Selector="collectionView:numberOfItemsInSection:")]
[Foundation.ProtocolMember(IsProperty=false, IsRequired=true, IsStatic=false, Name="GetCell", ParameterByRef=new System.Boolean[] { false, false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView), typeof(Foundation.NSIndexPath) }, ReturnType=typeof(UIKit.UICollectionViewCell), Selector="collectionView:cellForItemAtIndexPath:")]
[Foundation.ProtocolMember(IsProperty=false, IsRequired=false, IsStatic=false, Name="NumberOfSections", ParameterByRef=new System.Boolean[] { false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView) }, ReturnType=typeof(System.IntPtr), Selector="numberOfSectionsInCollectionView:")]
[Foundation.ProtocolMember(IsProperty=false, IsRequired=false, IsStatic=false, Name="GetViewForSupplementaryElement", ParameterByRef=new System.Boolean[] { false, false, false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView), typeof(Foundation.NSString), typeof(Foundation.NSIndexPath) }, ReturnType=typeof(UIKit.UICollectionReusableView), Selector="collectionView:viewForSupplementaryElementOfKind:atIndexPath:")]
[Foundation.ProtocolMember(IsProperty=false, IsRequired=false, IsStatic=false, Name="CanMoveItem", ParameterByRef=new System.Boolean[] { false, false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView), typeof(Foundation.NSIndexPath) }, ReturnType=typeof(System.Boolean), Selector="collectionView:canMoveItemAtIndexPath:")]
[Foundation.ProtocolMember(IsProperty=false, IsRequired=false, IsStatic=false, Name="MoveItem", ParameterByRef=new System.Boolean[] { false, false, false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView), typeof(Foundation.NSIndexPath), typeof(Foundation.NSIndexPath) }, Selector="collectionView:moveItemAtIndexPath:toIndexPath:")]
[Foundation.ProtocolMember(IsProperty=false, IsRequired=false, IsStatic=false, Name="GetIndexTitles", ParameterByRef=new System.Boolean[] { false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView) }, ReturnType=typeof(System.String[]), Selector="indexTitlesForCollectionView:")]
[Foundation.ProtocolMember(IsProperty=false, IsRequired=false, IsStatic=false, Name="GetIndexPath", ParameterByRef=new System.Boolean[] { false, false, false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView), typeof(System.String), typeof(System.IntPtr) }, ReturnType=typeof(Foundation.NSIndexPath), Selector="collectionView:indexPathForIndexTitle:atIndex:")]
public interface IUICollectionViewDataSource : IDisposable, ObjCRuntime.INativeObject
[<Foundation.Protocol(Name="UICollectionViewDataSource", WrapperType=typeof(UIKit.UICollectionViewDataSourceWrapper))>]
[<Foundation.ProtocolMember(IsProperty=false, IsRequired=true, IsStatic=false, Name="GetItemsCount", ParameterByRef=new System.Boolean[] { false, false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView), typeof(System.IntPtr) }, ReturnType=typeof(System.IntPtr), Selector="collectionView:numberOfItemsInSection:")>]
[<Foundation.ProtocolMember(IsProperty=false, IsRequired=true, IsStatic=false, Name="GetCell", ParameterByRef=new System.Boolean[] { false, false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView), typeof(Foundation.NSIndexPath) }, ReturnType=typeof(UIKit.UICollectionViewCell), Selector="collectionView:cellForItemAtIndexPath:")>]
[<Foundation.ProtocolMember(IsProperty=false, IsRequired=false, IsStatic=false, Name="NumberOfSections", ParameterByRef=new System.Boolean[] { false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView) }, ReturnType=typeof(System.IntPtr), Selector="numberOfSectionsInCollectionView:")>]
[<Foundation.ProtocolMember(IsProperty=false, IsRequired=false, IsStatic=false, Name="GetViewForSupplementaryElement", ParameterByRef=new System.Boolean[] { false, false, false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView), typeof(Foundation.NSString), typeof(Foundation.NSIndexPath) }, ReturnType=typeof(UIKit.UICollectionReusableView), Selector="collectionView:viewForSupplementaryElementOfKind:atIndexPath:")>]
[<Foundation.ProtocolMember(IsProperty=false, IsRequired=false, IsStatic=false, Name="CanMoveItem", ParameterByRef=new System.Boolean[] { false, false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView), typeof(Foundation.NSIndexPath) }, ReturnType=typeof(System.Boolean), Selector="collectionView:canMoveItemAtIndexPath:")>]
[<Foundation.ProtocolMember(IsProperty=false, IsRequired=false, IsStatic=false, Name="MoveItem", ParameterByRef=new System.Boolean[] { false, false, false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView), typeof(Foundation.NSIndexPath), typeof(Foundation.NSIndexPath) }, Selector="collectionView:moveItemAtIndexPath:toIndexPath:")>]
[<Foundation.ProtocolMember(IsProperty=false, IsRequired=false, IsStatic=false, Name="GetIndexTitles", ParameterByRef=new System.Boolean[] { false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView) }, ReturnType=typeof(System.String[]), Selector="indexTitlesForCollectionView:")>]
[<Foundation.ProtocolMember(IsProperty=false, IsRequired=false, IsStatic=false, Name="GetIndexPath", ParameterByRef=new System.Boolean[] { false, false, false }, ParameterType=new System.Type[] { typeof(UIKit.UICollectionView), typeof(System.String), typeof(System.IntPtr) }, ReturnType=typeof(Foundation.NSIndexPath), Selector="collectionView:indexPathForIndexTitle:atIndex:")>]
type IUICollectionViewDataSource = interface
    interface INativeObject
    interface IDisposable
Derived
Attributes
Implements

Remarks

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 GetCell(UICollectionView, NSIndexPath) and GetViewForSupplementaryElement(UICollectionView, NSString, NSIndexPath) are the functions that mutate the values of UICollectionReusableView component views. Because these methods may be called frequently during scrolling (particularly GetCell(UICollectionView, NSIndexPath)), 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 GetCell(UICollectionView, NSIndexPath) method sets this property in the reused animalCell from the animals data as appropriate. Similarly, the UICollectionViewDataSource sets the Text property of a Header type defined in the project.

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 UICollectionViewSource combines the UICollectionViewDataSource API and the UICollectionViewDelegate API in a single convenience class. Rather than creating two classes to assign to the DataSource and Delegate properties, a single UICollectionViewSource can be created and assigned to the Source property.

Properties

Name Description
Handle

Handle (pointer) to the unmanaged object representation.

(Inherited from INativeObject)

Methods

Name Description
CanMoveItem(UICollectionView, NSIndexPath)
GetCell(UICollectionView, NSIndexPath)

Gets a cell.

GetIndexPath(UICollectionView, String, IntPtr)

Requests the index path for the item in the collection view at the specified index with the specified title.

GetIndexTitles(UICollectionView)

Requests the index titles for the items in the specified collection view.

GetItemsCount(UICollectionView, IntPtr)

Returns the number of items in the specified section.

GetViewForSupplementaryElement(UICollectionView, NSString, NSIndexPath)

The reusable view used for the supplementary element at the specified indexPath.

MoveItem(UICollectionView, NSIndexPath, NSIndexPath)
NumberOfSections(UICollectionView)

The number of sections in this UICollectionViewDataSource.

Extension Methods

Name Description
GetHandle(INativeObject)
GetNonNullHandle(INativeObject, String)
CanMoveItem(IUICollectionViewDataSource, UICollectionView, NSIndexPath)
GetIndexPath(IUICollectionViewDataSource, UICollectionView, String, IntPtr)

Requests the index path for the item in the collection view at the specified index with the specified title.

GetIndexTitles(IUICollectionViewDataSource, UICollectionView)

Requests the index titles for the items in the specified collection view.

GetViewForSupplementaryElement(IUICollectionViewDataSource, UICollectionView, NSString, NSIndexPath)

The reusable view used for the supplementary element at the specified indexPath.

MoveItem(IUICollectionViewDataSource, UICollectionView, NSIndexPath, NSIndexPath)
NumberOfSections(IUICollectionViewDataSource, UICollectionView)

The number of sections in this UICollectionViewDataSource.

Applies to

See also