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

See Also: UICollectionViewCell Members


[Foundation.Register("UICollectionViewCell", true)]
public class UICollectionViewCell : UICollectionReusableView

See Also



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

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

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

Each cell is an instance of the UIKit.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 UIKit.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 UIKit.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 UIKit.UICollectionViewCell while the animal variable refers to an instance of a model class. The UICollectionViewSource.GetCell method sets visual aspects of the UIKit.UICollectionViewCell to conform to the animal data, in this case, setting an Image property appropriately.

C# Example

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;

Related content


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