Skip to content

Instantly share code, notes, and snippets.

@mikebluestein
Created July 28, 2014 20:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikebluestein/47ba7b7ca4ce65f36b41 to your computer and use it in GitHub Desktop.
Save mikebluestein/47ba7b7ca4ce65f36b41 to your computer and use it in GitHub Desktop.
UICollectionView using a UIViewController
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace CollectionViewWithControllerDemo
{
public class Controller : UIViewController
{
CVSource source;
UICollectionViewFlowLayout layout;
UICollectionView collectionView;
public Controller ()
{
layout = new UICollectionViewFlowLayout {
SectionInset = new UIEdgeInsets (20, 5, 5, 5),
MinimumInteritemSpacing = 5,
MinimumLineSpacing = 5,
ItemSize = new SizeF (100, 100)
};
collectionView = new UICollectionView (UIScreen.MainScreen.Bounds, layout);
collectionView.ContentSize = View.Frame.Size;
source = new CVSource ();
collectionView.RegisterClassForCell (typeof(TextCell), TextCell.CellId);
collectionView.Source = source;
}
public override void LoadView ()
{
base.LoadView ();
View = collectionView;
}
class CVSource : UICollectionViewSource
{
string[] data = { "one", "two", "three", "four" };
public override int GetItemsCount (UICollectionView collectionView, int section)
{
return data.Length;
}
public override UICollectionViewCell GetCell (UICollectionView collectionView, NSIndexPath indexPath)
{
var textCell = (TextCell)collectionView.DequeueReusableCell (TextCell.CellId, indexPath);
textCell.Text = data [indexPath.Row];
return textCell;
}
public override void ItemSelected (UICollectionView collectionView, NSIndexPath indexPath)
{
Console.WriteLine ("Row {0} selected", indexPath.Row);
}
public override bool ShouldSelectItem (UICollectionView collectionView, NSIndexPath indexPath)
{
return true;
}
}
class TextCell : UICollectionViewCell
{
UILabel label;
public static readonly NSString CellId = new NSString ("TextCell");
public string Text {
get {
return label.Text;
}
set {
label.Text = value;
SetNeedsDisplay ();
}
}
[Export ("initWithFrame:")]
TextCell (RectangleF frame) : base (frame)
{
label = new UILabel (ContentView.Frame) {
BackgroundColor = UIColor.Red,
TextColor = UIColor.Blue,
TextAlignment = UITextAlignment.Center
};
ContentView.AddSubview (label);
}
}
}
}
@aimore
Copy link

aimore commented Aug 15, 2017

Hi @mikebluestein, how to implement the UICollectionViewDataSource?
I am trying to use this (https://gist.github.com/aimore/2b95a8f561e09402c2610394c251c122) as XF CustomRenderer

@rraallvv
Copy link

rraallvv commented Feb 3, 2018

@mikebluestein for some reason the text labels inside the TextCell are not showing up. I tried changing the background color for the collection and cell, and those changes are reflected o the screen, but that's not the case for the labels.

@chartierpw
Copy link

chartierpw commented Jul 21, 2020

@mikebluestein All I get is a black screen, no text/label, just solid black ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment