iOS Guides | Samples

MonoTouch.UIKit.UICollectionViewCell Class

Represents a reusable content view in the set being presented by the UICollectionView.

See Also: UICollectionViewCell

Syntax

[MonoTouch.Foundation.Register("UICollectionViewCell", true)]
[MonoTouch.ObjCRuntime.Introduced(MonoTouch.ObjCRuntime.PlatformName.iOS, 6, 0, MonoTouch.ObjCRuntime.PlatformArchitecture.None, null)]
public class UICollectionViewCell : UICollectionReusableView

See Also

Remarks

UICollectionViewCells are reusable views that present a single item in the data set that is being presented by the collection view. As subtypes of UICollectionReusableView, they are expected to be managed by their containing UICollectionView.

The UICollectionView class is designed to work with large datasets while respecting the resource limitations of iOS devices. To do this, the UICollectionView maintains a reuse queue of component views of type UICollectionReusableView, instantiating only as many as are strictly necessary and replacing values in UICollectionViewCells that are no longer visible with values appropriate to UICollectionViewCells that are about to scroll onto the screen.

The reuse queue implies an unusual lifecycle for UICollectionViewCells (and other subtypes of UICollectionReusableView). Rather than the application developer allocating them and allowing the garbage collector to scavenge them appropriately, UICollectionReusableViews are allocated by a UICollectionView and those that are no longer on-screen are reused without deallocation. (See UICollectionView for more discussion.)

Each cell is an instance of the UICollectionViewCell class, which is comprised of three different views, as shown in the figure below, taken from the "Introduction to Collection Views" guide:

The Cells in the screenshot above are created by inheriting from UICollectionView and setting the UICollectionView.ContentView, UICollectionView.ContentView, and UICollectionView.BackgroundView properties as shown in the following code:

C# Example

          BackgroundView = new UIView{BackgroundColor = UIColor.Orange};
          
          SelectedBackgroundView = new UIView{BackgroundColor = UIColor.Green};
          
          ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
          ContentView.Layer.BorderWidth = 2.0f;
          ContentView.BackgroundColor = UIColor.White;
          ContentView.Transform = CGAffineTransform.MakeScale (0.8f, 0.8f);
          
          imageView = new UIImageView (UIImage.FromBundle ("placeholder.png"));
          imageView.Center = ContentView.Center;
          imageView.Transform = CGAffineTransform.MakeScale (0.7f, 0.7f);
          
          ContentView.AddSubview (imageView);

Since an individual instance of UICollectionViewCell may be reused without being re-allocated, the application developer may also have to adjust aspects dynamically using the UICollectionViewSource.GetCell method, as shown in the following code, taken from the "Introduction to Collection Views" sample. The animalCell is an instance of a subtype of UICollectionViewCell while the animal variable refers to an instance of a model class. The UICollectionViewSource.GetCell method sets visual aspects of the UICollectionViewCell to conform to the animal data, in this case, setting an Image property appropriately.

C# Example

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;
}

Related content

Requirements

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

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

See Also: UICollectionReusableView

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.
Initializes the UICollectionViewCell with the specified frame.
A constructor used when creating managed representations of unmanaged objects; Called by the runtime.

Public Properties

[read-only]
static
AppearanceUICollectionViewCell+UICollectionViewCellAppearance. Strongly-typed property that returns the UIAppearance class for this class.
BackgroundViewUIView. The UIView that is displayed behind the cell's ContentView.
[read-only]
override
ClassHandleIntPtr. The handle for this class.
[read-only]
ContentViewUIView. The main UIView displayed by the cell.
HighlightedBoolean. The highlight state of this UICollectionViewCell.
SelectedBoolean. The selection state of this UICollectionViewCell.
SelectedBackgroundViewUIView. The UIView that is displayed, above the BackgroundView, when the cell is selected.

Public Methods

static
AppearanceWhenContainedIn(params Type[]) : UICollectionViewCell+UICollectionViewCellAppearance
Returns a strongly typed UIAppearance for instances of this class when the view is hosted in the specified hierarchy.
static
GetAppearance(UITraitCollection) : UICollectionViewCell+UICollectionViewCellAppearance
Returns an appearance proxy for the specified traits.
static
GetAppearance(UITraitCollection, params Type[]) : UICollectionViewCell+UICollectionViewCellAppearance
Returns an appearance proxy for the specified traits when found in the containers containment hierarchy.
static
GetAppearance<T>() : UICollectionViewCell+UICollectionViewCellAppearance
Obtains the appearance proxy UICollectionViewCellAppearance for the subclass of UICollectionViewCell.
static
GetAppearance<T>(UITraitCollection) : UICollectionViewCell+UICollectionViewCellAppearance
Obtains the appearance proxy UICollectionViewCellAppearance for the subclass of UICollectionViewCell.
static
GetAppearance<T>(UITraitCollection) : UICollectionViewCell+UICollectionViewCellAppearance
Obtains the appearance proxy UICollectionViewCellAppearance for the subclass of UICollectionViewCell.
static
GetAppearance<T>(UITraitCollection, params Type[]) : UICollectionViewCell+UICollectionViewCellAppearance
Obtains the appearance proxy UICollectionViewCellAppearance for the subclass of UICollectionViewCell that has the specified trait collection when the view is hosted in the specified hierarchy.