Created
July 28, 2014 20:49
-
-
Save mikebluestein/47ba7b7ca4ce65f36b41 to your computer and use it in GitHub Desktop.
UICollectionView using a UIViewController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} | |
@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.
@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
Hi @mikebluestein, how to implement the UICollectionViewDataSource?
I am trying to use this (https://gist.github.com/aimore/2b95a8f561e09402c2610394c251c122) as XF CustomRenderer