SlideShare a Scribd company logo
1 of 30
Introduction to
Collection Views


    November 1, 2012

     Copyright 2012 © Xamarin Inc. All rights reserved
Mike Bluestein
                    Xamarin Documentation Team
                    mike.bluestein@xamarin.com
                    @mikebluestein




                                                    Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
UICOLLECTIONVIEW


• New     in iOS 6

• Items   displayed with layout

• Easy   to implement grid of items
UICOLLECTIONVIEW


• Data   is provided via Data Source

• Interaction   is handled though Delegate

• Very   similar to UITableView

• Manages   cell selection
UICOLLECTIONVIEW


• Cells

• Supplementary Views

• Decoration Views
CELLS

• UICollectionViewCell

• Represents      a single item in the data set

• Has   3 parts                           BackgroundView
                                           SelectedBackgroundView
 • ContentView
                                             ContentView

 • SelectedBackgroundView

 • BackgroundView
UICOLLECTIONVIEWCELL
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);
      }
  }
CELL REUSE

• Nolonger 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
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;
}
SUPPLEMENTARY VIEWS


• Driven   by section data

•A   more general way of doing headers and footers

• Can   use any view to create
SUPPLEMENTARY VIEWS
SUPPLEMENTARY VIEWS


• Register   Supplementary Views similar to Cells

• GetViewForSupplementaryElement       returns view

• DequeueReusableSupplementary       view creates view

• Supplementary View     inherits from UICollectionReusableView
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;
}
DECORATION VIEWS

• Provide   visual content

• Not   data driven

• Common      use to provide backdrop

• Associated   with layout

• Created   in layout subclass
DECORATION VIEWS
DECORATION VIEWS

• Must   inherit from UICollectionViewReusableView

           public class MyDecorationView : UICollectionReusableView
           {
                [Export ("initWithFrame:")]
                public MyDecorationView (System.Drawing.RectangleF frame) : base (frame)
                {
                      BackgroundColor = UIColor.Red;
                }
           }
CREATE DECORATION VIEWS

• RegisterClassForDecorationView                           in layout class
   RegisterClassForDecorationView (typeof(MyDecorationView), myDecorationViewId);




• UICollectionViewLayoutAttributes.CreateForDecorationView

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



• UICollectionViewDataSource   is used to provide data

• Works   similar to data source used to populate UITableView
MORE ABOUT CELL REUSE
HANDLING INTERACTION


• UICollectionViewDelegate   handles item interaction

 • Item   Selection

 • Highlighting

 • Context   Menu
CONTROLLER

• UICollectionViewController

• Pre-defined   controller

• View   is a UICollectionView

• Usageis optional (like UITableView can also use any
 UIViewController as the controller for a UICollectionView)
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 from UICollectionViewLayout
FLOW LAYOUT

• UICollectionViewFlowLayout

• Line-based   layout

• For   example, grids

• Automatic    handling of
 orientation
FLOW LAYOUT


• Customline-based layouts
 beyond grids

• Caninherit from
 UICollectionViewFlowLayout
FLOW LAYOUT DELEGATE

• UICollectionViewDelegateFlowLayout

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

  • ItemSize, MinimumLineSpacing, MinimumInteritemSpacing,
   etc...

• Defaults   to class set for UICollectionView.Delegate
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
CUSTOM LAYOUT
DEMO
Xamarin
    Seminar
   Please give us your feedback
  http://bit.ly/xamfeedback


      Follow us on Twitter
        @XamarinHQ



        Copyright 2012 © Xamarin Inc. All rights reserved

More Related Content

More from Xamarin

More from Xamarin (20)

Exploring UrhoSharp 3D with Xamarin Workbooks
Exploring UrhoSharp 3D with Xamarin WorkbooksExploring UrhoSharp 3D with Xamarin Workbooks
Exploring UrhoSharp 3D with Xamarin Workbooks
 
Desktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
Desktop Developer’s Guide to Mobile with Visual Studio Tools for XamarinDesktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
Desktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
 
Developer’s Intro to Azure Machine Learning
Developer’s Intro to Azure Machine LearningDeveloper’s Intro to Azure Machine Learning
Developer’s Intro to Azure Machine Learning
 
Customizing Xamarin.Forms UI
Customizing Xamarin.Forms UICustomizing Xamarin.Forms UI
Customizing Xamarin.Forms UI
 
Session 4 - Xamarin Partner Program, Events and Resources
Session 4 - Xamarin Partner Program, Events and ResourcesSession 4 - Xamarin Partner Program, Events and Resources
Session 4 - Xamarin Partner Program, Events and Resources
 
Session 3 - Driving Mobile Growth and Profitability
Session 3 - Driving Mobile Growth and ProfitabilitySession 3 - Driving Mobile Growth and Profitability
Session 3 - Driving Mobile Growth and Profitability
 
Session 2 - Emerging Technologies in your Mobile Practice
Session 2 - Emerging Technologies in your Mobile PracticeSession 2 - Emerging Technologies in your Mobile Practice
Session 2 - Emerging Technologies in your Mobile Practice
 
Session 1 - Transformative Opportunities in Mobile and Cloud
Session 1 - Transformative Opportunities in Mobile and Cloud Session 1 - Transformative Opportunities in Mobile and Cloud
Session 1 - Transformative Opportunities in Mobile and Cloud
 
SkiaSharp Graphics for Xamarin.Forms
SkiaSharp Graphics for Xamarin.FormsSkiaSharp Graphics for Xamarin.Forms
SkiaSharp Graphics for Xamarin.Forms
 
Building Games for iOS, macOS, and tvOS with Visual Studio and Azure
Building Games for iOS, macOS, and tvOS with Visual Studio and AzureBuilding Games for iOS, macOS, and tvOS with Visual Studio and Azure
Building Games for iOS, macOS, and tvOS with Visual Studio and Azure
 
Intro to Xamarin.Forms for Visual Studio 2017
Intro to Xamarin.Forms for Visual Studio 2017Intro to Xamarin.Forms for Visual Studio 2017
Intro to Xamarin.Forms for Visual Studio 2017
 
Connected Mobile Apps with Microsoft Azure
Connected Mobile Apps with Microsoft AzureConnected Mobile Apps with Microsoft Azure
Connected Mobile Apps with Microsoft Azure
 
Introduction to Xamarin for Visual Studio 2017
Introduction to Xamarin for Visual Studio 2017Introduction to Xamarin for Visual Studio 2017
Introduction to Xamarin for Visual Studio 2017
 
Building Your First iOS App with Xamarin for Visual Studio
Building Your First iOS App with Xamarin for Visual StudioBuilding Your First iOS App with Xamarin for Visual Studio
Building Your First iOS App with Xamarin for Visual Studio
 
Building Your First Android App with Xamarin
Building Your First Android App with XamarinBuilding Your First Android App with Xamarin
Building Your First Android App with Xamarin
 
Building Your First Xamarin.Forms App
Building Your First Xamarin.Forms AppBuilding Your First Xamarin.Forms App
Building Your First Xamarin.Forms App
 
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
 
Xamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOps
Xamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOpsXamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOps
Xamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOps
 
Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...
Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...
Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...
 
Xamarin Mobile Leaders Summit: Business at the Point of Inspiration: Producti...
Xamarin Mobile Leaders Summit: Business at the Point of Inspiration: Producti...Xamarin Mobile Leaders Summit: Business at the Point of Inspiration: Producti...
Xamarin Mobile Leaders Summit: Business at the Point of Inspiration: Producti...
 

Recently uploaded

Recently uploaded (20)

Designing for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastDesigning for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at Comcast
 
ECS 2024 Teams Premium - Pretty Secure
ECS 2024   Teams Premium - Pretty SecureECS 2024   Teams Premium - Pretty Secure
ECS 2024 Teams Premium - Pretty Secure
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
Agentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdfAgentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdf
 
The UX of Automation by AJ King, Senior UX Researcher, Ocado
The UX of Automation by AJ King, Senior UX Researcher, OcadoThe UX of Automation by AJ King, Senior UX Researcher, Ocado
The UX of Automation by AJ King, Senior UX Researcher, Ocado
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 

Introduction to Collection Views in iOS 6

  • 1. Introduction to Collection Views November 1, 2012 Copyright 2012 © Xamarin Inc. All rights reserved
  • 2. Mike Bluestein Xamarin Documentation Team mike.bluestein@xamarin.com @mikebluestein Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 3. UICOLLECTIONVIEW • New in iOS 6 • Items displayed with layout • Easy to implement grid of items
  • 4. UICOLLECTIONVIEW • Data is provided via Data Source • Interaction is handled though Delegate • Very similar to UITableView • Manages cell selection
  • 5. UICOLLECTIONVIEW • Cells • Supplementary Views • Decoration Views
  • 6. CELLS • UICollectionViewCell • Represents a single item in the data set • Has 3 parts BackgroundView SelectedBackgroundView • ContentView ContentView • SelectedBackgroundView • BackgroundView
  • 8. 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); } }
  • 9. CELL REUSE • Nolonger 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
  • 10. 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; }
  • 11. SUPPLEMENTARY VIEWS • Driven by section data •A more general way of doing headers and footers • Can use any view to create
  • 13. SUPPLEMENTARY VIEWS • Register Supplementary Views similar to Cells • GetViewForSupplementaryElement returns view • DequeueReusableSupplementary view creates view • Supplementary View inherits from UICollectionReusableView
  • 14. 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; }
  • 15. DECORATION VIEWS • Provide visual content • Not data driven • Common use to provide backdrop • Associated with layout • Created in layout subclass
  • 17. DECORATION VIEWS • Must inherit from UICollectionViewReusableView public class MyDecorationView : UICollectionReusableView { [Export ("initWithFrame:")] public MyDecorationView (System.Drawing.RectangleF frame) : base (frame) { BackgroundColor = UIColor.Red; } }
  • 18. CREATE DECORATION VIEWS • RegisterClassForDecorationView in layout class RegisterClassForDecorationView (typeof(MyDecorationView), myDecorationViewId); • UICollectionViewLayoutAttributes.CreateForDecorationView var decorationAttribs = UICollectionViewLayoutAttributes.CreateForDecorationView (myDecorationViewId, NSIndexPath.FromItemSection (0, 0)); decorationAttribs.Size = rect.Size; decorationAttribs.Center = CollectionView.Center; decorationAttribs.ZIndex = -1;
  • 19. PROVIDING CONTENT • UICollectionViewDataSource is used to provide data • Works similar to data source used to populate UITableView
  • 21. HANDLING INTERACTION • UICollectionViewDelegate handles item interaction • Item Selection • Highlighting • Context Menu
  • 22. CONTROLLER • UICollectionViewController • Pre-defined controller • View is a UICollectionView • Usageis optional (like UITableView can also use any UIViewController as the controller for a UICollectionView)
  • 23. 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 from UICollectionViewLayout
  • 24. FLOW LAYOUT • UICollectionViewFlowLayout • Line-based layout • For example, grids • Automatic handling of orientation
  • 25. FLOW LAYOUT • Customline-based layouts beyond grids • Caninherit from UICollectionViewFlowLayout
  • 26. FLOW LAYOUT DELEGATE • UICollectionViewDelegateFlowLayout • Allowslayout attributes to be set granularly per section and item rather than globally • ItemSize, MinimumLineSpacing, MinimumInteritemSpacing, etc... • Defaults to class set for UICollectionView.Delegate
  • 27. 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
  • 29. DEMO
  • 30. Xamarin Seminar Please give us your feedback http://bit.ly/xamfeedback Follow us on Twitter @XamarinHQ Copyright 2012 © Xamarin Inc. All rights reserved

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n