31
INTRODUCTION TO COLLECTION VIEWS Using MonoTouch and C# Mike Bluestein Xamarin 11/7/2012

Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

INTRODUCTION TO COLLECTION VIEWS

Using MonoTouch and C#

Mike BluesteinXamarin

11/7/2012

Page 2: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

MONOTOUCH

• Platform for creating iOS application with C# from Xamarin

• Bindings to native APIs

• Reuse of C# code and base class libraries

• AOT C# compilation

•Memory management

Page 3: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

GETTING STARTED SEMINAR

http://youtu.be/LYhXuhMnke8

Page 4: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

UICOLLECTIONVIEW

•New in iOS 6

• Items displayed with layout

• Easy to implement grid of items

Page 5: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

UICOLLECTIONVIEW

•Data is provided via Data Source

• Interaction is handled though Delegate

• Very similar to UITableView

•Manages cell selection

Page 6: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

UICOLLECTIONVIEW

• Cells

• Supplementary Views

•Decoration Views

Page 7: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

CELLS

• UICollectionViewCell

• Represents a single item in the data set

• Has 3 parts

• ContentView

• SelectedBackgroundView

• BackgroundView

BackgroundView

SelectedBackgroundView

ContentView

Page 8: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

UICOLLECTIONVIEWCELL

Page 9: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

UICOLLECTIONVIEWCELL

public class AnimalCell : UICollectionViewCell{ UIImageView imageView;

[Export ("initWithFrame:")] public AnimalCell (System.Drawing.RectangleF frame) : base (frame) { 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 ("image.png")); imageView.Center = ContentView.Center; imageView.Transform = CGAffineTransform.MakeScale (0.7f, 0.7f);

ContentView.AddSubview (imageView); }}

Page 10: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

CELL REUSE

•No longer have to check if cell exists before de-queueing in Delegate (like pre-iOS 6 UITableView)

• Register class or xib for cell

• System will create in DequeueReusableCell call

•Works with UICollectionView and UITableView

Page 11: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

CELL REUSE

CollectionView.RegisterClassForCell (typeof(MyCell), myCellId);

...

public override UICollectionViewCell GetCell (UICollectionView collectionView, MonoTouch.Foundation.NSIndexPath indexPath){ var myCell = (MyCell)collectionView.DequeueReusableCell (myCellId, indexPath);

var myObject = data [indexPath.Row];

// populate myCell with data from myObject

...

return myCell;}

Page 12: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

SUPPLEMENTARY VIEWS

•Driven by section data

• A more general way of doing headers and footers

• Can use any view to create

Page 13: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

SUPPLEMENTARY VIEWS

Page 14: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

SUPPLEMENTARY VIEWS

• Register Supplementary Views similar to Cells

• GetViewForSupplementaryElement returns view

•DequeueReusableSupplementaryView creates view

• Supplementary View inherits from UICollectionReusableView

Page 15: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

SUPPLEMENTARY VIEWS

CollectionView.RegisterClassForSupplementaryView (typeof(Header), UICollectionElementKindSection.Header,headerId);

public override UICollectionReusableView GetViewForSupplementaryElement (UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)

{ var headerView = (Header)collectionView.DequeueReusableSupplementaryView (

elementKind, headerId, indexPath);

headerView.Text = "This is a Supplementary View";

return headerView;}

Page 16: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

DECORATION VIEWS

• Provide visual content

•Not data driven

• Common use to provide backdrop

• Associated with layout

• Created in layout subclass

Page 17: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

DECORATION VIEWS

Page 18: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

DECORATION VIEWS

•Must inherit from UICollectionViewReusableViewpublic class MyDecorationView : UICollectionReusableView{ [Export ("initWithFrame:")] public MyDecorationView (System.Drawing.RectangleF frame) : base (frame) { BackgroundColor = UIColor.Black;

... }}

Page 19: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

CREATE DECORATION VIEWS

• RegisterClassForDecorationView in layout class

• UICollectionViewLayoutAttributes.CreateForDecorationView

RegisterClassForDecorationView (typeof(MyDecorationView), myDecorationViewId);

var decorationAttribs = UICollectionViewLayoutAttributes.CreateForDecorationView (myDecorationViewId, NSIndexPath.FromItemSection (0, 0));decorationAttribs.Size = rect.Size;decorationAttribs.Center = CollectionView.Center;decorationAttribs.ZIndex = -1;

Page 20: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

PROVIDING CONTENT

• UICollectionViewDataSource is used to provide data

•Works similar to data source used to populate UITableView

Page 21: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

MORE ABOUT CELL REUSE

Page 22: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

HANDLING INTERACTION

• UICollectionViewDelegate handles item interaction

• Item Selection

• Highlighting

• Context Menu

Page 23: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

CONTROLLER

• UICollectionViewController

• Pre-defined controller

• View is a UICollectionView

• Usage is optional (like UITableView can also use any UIViewController as the controller for a UICollectionView)

Page 24: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

LAYOUT

• UICollectionViewLayout - base class for any layout

• Creates layout attributes for every item, supplementary view and decoration view

• Built-in UICollectionViewFlowLayout

• Custom layout - inherit directly form UICollectionViewLayout

Page 25: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

FLOW LAYOUT

• UICollectionViewFlowLayout

• Line-based layout

• For example, grids

• Automatic handling of orientation

Page 26: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

FLOW LAYOUT

• Custom line-based layouts beyond grids

• Can inherit from UICollectionViewFlowLayout

Page 27: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

FLOW LAYOUT DELEGATE

• UICollectionViewDelegateFlowLayout

• Allows layout attributes to be set granularly per section and item rather than globally

• ItemSize, MinimumLineSpacing, MinimumInteritemSpacing, etc...

•Defaults to class set for UICollectionView.Delegate

Page 28: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

CUSTOM LAYOUT

• Create custom layouts that are not line-based

• Inherit directly from UICollectionViewLayout

•Override:

• PrepareLayout - initial layout calculations

• CollectionViewContentSize - display area

• LayoutAttributesForElementsInRect - position items

Page 29: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

CUSTOM LAYOUT

Page 30: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

DEMO

Page 31: Using MonoTouch and C# Mike Bluestein Xamarin 11/7 - Meetup

THANK YOU

[email protected]

•@mikebluestein

• docs.xamarin.com