what’s new in nscollectionview - apple inc....“stretchy” grid items all same size like...

270
© 2015 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. What’s New in NSCollectionView Troy Stephens Application Frameworks Engineer App Frameworks #WWDC15 Session 225

Upload: others

Post on 10-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

© 2015 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

What’s New in NSCollectionView

Troy Stephens Application Frameworks Engineer

App Frameworks #WWDC15

Session 225

Page 2: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionView

Page 3: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionView

Displays grids of identically sized items

Page 4: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionView

Displays grids of identically sized itemsEach item cloned from an “itemPrototype”

Page 5: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionView

Displays grids of identically sized itemsEach item cloned from an “itemPrototype”• ViewController + View subtree

Page 6: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionView

Displays grids of identically sized itemsEach item cloned from an “itemPrototype”• ViewController + View subtree

Supports selection, drag-and-drop, animated relayout

Page 7: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports
Page 8: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports
Page 9: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

UICollectionView

Page 10: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

UICollectionView

Displays collections of items

Page 11: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

UICollectionView

Displays collections of itemsEach item created from a .nib or view class

Page 12: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

UICollectionView

Displays collections of itemsEach item created from a .nib or view classCan mix item types

Page 13: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

UICollectionView

Displays collections of itemsEach item created from a .nib or view classCan mix item types

Page 14: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

UICollectionView

Displays collections of itemsEach item created from a .nib or view classCan mix item typesHeaders, footers

Page 15: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

UICollectionView

Displays collections of itemsEach item created from a .nib or view classCan mix item typesHeaders, footers

Page 16: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

UICollectionView

Displays collections of itemsEach item created from a .nib or view classCan mix item typesHeaders, footersSections

Page 17: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

UICollectionView

Displays collections of itemsEach item created from a .nib or view classCan mix item typesHeaders, footersSectionsFlexible, customizable “Flow” layout

Page 18: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

UICollectionView

Displays collections of itemsEach item created from a .nib or view classCan mix item typesHeaders, footersSectionsFlexible, customizable “Flow” layoutDeveloper-definable layouts

Page 19: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

UICollectionView

Displays collections of itemsEach item created from a .nib or view classCan mix item typesHeaders, footersSectionsFlexible, customizable “Flow” layoutDeveloper-definable layoutsScalable to large numbers of items

Page 20: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

UICollectionView

Displays collections of itemsEach item created from a .nib or view classCan mix item typesHeaders, footersSectionsFlexible, customizable “Flow” layoutDeveloper-definable layoutsScalable to large numbers of itemsLayer-backed

Page 21: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

A Better NSCollectionView

Page 22: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

A Better NSCollectionView

Scalable

Page 23: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

A Better NSCollectionView

ScalableOptional sectioning, with header/footer views

Page 24: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

A Better NSCollectionView

ScalableOptional sectioning, with header/footer viewsCompletely customizable layout

Page 25: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

A Better NSCollectionView

ScalableOptional sectioning, with header/footer viewsCompletely customizable layoutHeterogeneous and variable-sized items

Page 26: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

A Better NSCollectionView

ScalableOptional sectioning, with header/footer viewsCompletely customizable layoutHeterogeneous and variable-sized itemsCustomizable appearance

Page 27: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

A Better NSCollectionView

ScalableOptional sectioning, with header/footer viewsCompletely customizable layoutHeterogeneous and variable-sized itemsCustomizable appearanceAnimation control

Page 28: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

OS X Adaptations

Page 29: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

OS X Adaptations

Drag and Drop, including Layout-based drop target determination and indication

Page 30: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

OS X Adaptations

Drag and Drop, including Layout-based drop target determination and indicationRubberband drag-select

Page 31: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

OS X Adaptations

Drag and Drop, including Layout-based drop target determination and indicationRubberband drag-selectBulk selection and highlight notifications

Page 32: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

OS X Adaptations

Drag and Drop, including Layout-based drop target determination and indicationRubberband drag-selectBulk selection and highlight notificationsItems still represented using ViewControllers (NSCollectionViewItem)

Page 33: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

OS X Adaptations

Drag and Drop, including Layout-based drop target determination and indicationRubberband drag-selectBulk selection and highlight notificationsItems still represented using ViewControllers (NSCollectionViewItem)Automatic nib/class finding, via naming conventions

Page 34: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Today’s Goals

Page 35: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Today’s Goals

Learn how to wire up and use a new NSCollectionView

Page 36: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Today’s Goals

Learn how to wire up and use a new NSCollectionViewLearn what’s different on OS X vs. iOS

Page 37: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Today’s Goals

Learn how to wire up and use a new NSCollectionViewLearn what’s different on OS X vs. iOSA little something for everybody

Page 38: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Today’s Goals

Learn how to wire up and use a new NSCollectionViewLearn what’s different on OS X vs. iOSA little something for everybody• iOS developers bringing companion apps to OS X

Page 39: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Today’s Goals

Learn how to wire up and use a new NSCollectionViewLearn what’s different on OS X vs. iOSA little something for everybody• iOS developers bringing companion apps to OS X• OS X developers wanting to leverage the new capabilities

Page 40: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Today’s Goals

Learn how to wire up and use a new NSCollectionViewLearn what’s different on OS X vs. iOSA little something for everybody• iOS developers bringing companion apps to OS X• OS X developers wanting to leverage the new capabilities• Those new to all of this

Page 41: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

The Plan

Page 42: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

The Plan

Overview

Page 43: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

The Plan

OverviewNuts and bolts

Page 44: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

The Plan

OverviewNuts and boltsConclusion

Page 45: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Overview

Page 46: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Old API

Page 47: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionView

Old API

Page 48: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionView

Old API

content

NSArray / NSArrayController

Page 49: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewItem

NSCollectionView

Old APIitemPrototype

NSCollectionViewItem

view

content

NSArray / NSArrayController

Page 50: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewItem

NSCollectionView

Old APIitemPrototype

NSCollectionViewItem

view

content

NSArray / NSArrayController

delegate

Delegate

Page 51: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewItem

NSCollectionView

Old APIitemPrototype

NSCollectionViewItem

view

content

NSArray / NSArrayController

delegate

DelegateDrag and Drop

Page 52: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

New APIitemPrototype

Drag-and-Drop

content

NSArray / NSArrayController

delegate

Delegate

NSCollectionViewItem

view

NSCollectionView

Page 53: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

New APIitemPrototype

Drag-and-Drop

content

NSArray / NSArrayController

delegate

Delegate

NSCollectionViewItem

view

NSCollectionView

Page 54: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Two Required Methods

New APIitemPrototype

Drag-and-Drop

dataSource

Data Source

delegate

Delegate

NSCollectionViewItem

view

NSCollectionView

Page 55: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Two Required Methods

New APIitemPrototype

Drag-and-DropSelection and Highlighting

dataSource

Data Source

delegate

Delegate

NSCollectionViewItem

view

NSCollectionView

Page 56: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Two Required Methods

New API

Slide.xib

Drag-and-DropSelection and Highlighting

dataSource

Data Source

delegate

Delegate

NSCollectionViewItem

view

NSCollectionView

Page 57: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Two Required Methods

Negative.xibInstamatic.xib

New API

Slide.xib

Drag-and-DropSelection and Highlighting

dataSource

Data Source

delegate

Delegate

NSCollectionViewItem

view

NSCollectionView

Page 58: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Two Required Methods

Negative.xibInstamatic.xib

New API

NSCollectionViewLayout

collectionViewLayout

Slide.xib

Drag-and-DropSelection and Highlighting

dataSource

Data Source

delegate

Delegate

NSCollectionViewItem

view

NSCollectionView

Page 59: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Two Required Methods

Negative.xibInstamatic.xib

New API

NSCollectionViewLayout

collectionViewLayout

Slide.xib

Drag-and-DropSelection and Highlighting

Per-Item Layout Adjustments

dataSource

Data Source

delegate

Delegate

NSCollectionViewItem

view

NSCollectionView

Page 60: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Sizing and positioning items

NSCollectionViewLayout

Layouts

Page 61: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewGridLayout

Sizing and positioning items

NSCollectionViewLayout

Layouts

Page 62: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewGridLayout

Sizing and positioning items

“Stretchy” grid

NSCollectionViewLayout

Layouts

Page 63: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewGridLayout

Sizing and positioning items

“Stretchy” gridItems all same size

NSCollectionViewLayout

Layouts

Page 64: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewGridLayout

Sizing and positioning items

“Stretchy” gridItems all same size

No sections/headers/footers

NSCollectionViewLayout

Layouts

Page 65: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewGridLayout

Sizing and positioning items

NSCollectionViewGridLayout

“Stretchy” gridItems all same size

No sections/headers/footers

NSCollectionViewLayout

Layouts

Page 66: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewGridLayout

Sizing and positioning items

NSCollectionViewGridLayout

“Stretchy” gridItems all same size

No sections/headers/footers

NSCollectionViewLayout

NSCollectionViewFlowLayout

Layouts

Page 67: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewGridLayout

Sizing and positioning items

NSCollectionViewGridLayout

“Stretchy” gridItems all same size

Like flowing text fragments/CSS boxes

No sections/headers/footers

NSCollectionViewLayout

NSCollectionViewFlowLayout

Layouts

Page 68: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewGridLayout

Sizing and positioning items

NSCollectionViewGridLayout

“Stretchy” gridItems all same size

Like flowing text fragments/CSS boxesVariable item size

No sections/headers/footers

NSCollectionViewLayout

NSCollectionViewFlowLayout

Layouts

Page 69: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewGridLayout

Sizing and positioning items

NSCollectionViewGridLayout

“Stretchy” gridItems all same size

Like flowing text fragments/CSS boxesVariable item size

No sections/headers/footers Supports sections/headers/footers

NSCollectionViewLayout

NSCollectionViewFlowLayout

Layouts

Page 70: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewGridLayout

Sizing and positioning items

NSCollectionViewGridLayout

“Stretchy” gridItems all same size

Like flowing text fragments/CSS boxesVariable item size

No sections/headers/footers Supports sections/headers/footersPowerful and customizable

NSCollectionViewLayout

NSCollectionViewFlowLayout

Layouts

Page 71: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewGridLayout

Sizing and positioning items

Your Layout Here?

NSCollectionViewGridLayout

“Stretchy” gridItems all same size

No sections/headers/footers

NSCollectionViewLayout

NSCollectionViewFlowLayout

Layouts

Page 72: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewGridLayout

Sizing and positioning items

Your Layout Here?

Your Layout Here?

NSCollectionViewGridLayout

“Stretchy” gridItems all same size

No sections/headers/footers

NSCollectionViewLayout

NSCollectionViewFlowLayout

Layouts

Page 73: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Understanding “Layout Attributes” ObjectsNSCollectionViewLayoutAttributes

Page 74: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Understanding “Layout Attributes” ObjectsNSCollectionViewLayoutAttributes

Encapsulates frame, alphaValue, and other states that can be applied to a view

class NSCollectionViewLayoutAttributes : NSObject, NSCopying { var frame: NSRect var size: NSSize var alpha: CGFloat var zIndex: Int // default is 0 var hidden: Bool // As an optimization, NSCollectionView might not create a view for items whose hidden attribute is YES var indexPath: NSIndexPath?

Page 75: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Understanding “Layout Attributes” ObjectsNSCollectionViewLayoutAttributes

Encapsulates frame, alphaValue, and other states that can be applied to a viewEnables CollectionView APIs to reason about items not currently instantiated

class NSCollectionViewLayoutAttributes : NSObject, NSCopying { var frame: NSRect var size: NSSize var alpha: CGFloat var zIndex: Int // default is 0 var hidden: Bool // As an optimization, NSCollectionView might not create a view for items whose hidden attribute is YES var indexPath: NSIndexPath?

Page 76: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Understanding “Layout Attributes” ObjectsNSCollectionViewLayoutAttributes

Encapsulates frame, alphaValue, and other states that can be applied to a viewEnables CollectionView APIs to reason about items not currently instantiatedApplied to items/views at layout time

class NSCollectionViewLayoutAttributes : NSObject, NSCopying { var frame: NSRect var size: NSSize var alpha: CGFloat var zIndex: Int // default is 0 var hidden: Bool // As an optimization, NSCollectionView might not create a view for items whose hidden attribute is YES var indexPath: NSIndexPath?

Page 77: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping ItemsUsing sections

Page 78: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping ItemsUsing sections

Page 79: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping ItemsUsing sections

Page 80: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping ItemsUsing sections

Header

Page 81: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping ItemsUsing sections

Header

Footer

Page 82: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping ItemsUsing sections

Header

Footer

Page 83: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping ItemsUsing sections

Header

Footer

section 0

Page 84: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping ItemsUsing sections

Header

Footer

item 0 item 1 item 2 item 3 item 4

item 5 item 6 item 7

section 0

Page 85: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping ItemsUsing sections

Header

Footer

Header

item 0 item 1 item 2 item 3 item 4

item 5 item 6 item 7

section 0

Page 86: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping ItemsUsing sections

Header

Footer

Header

item 0 item 1 item 2 item 3 item 4

item 5 item 6 item 7

section 0

Page 87: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping ItemsUsing sections

Header

Footer

Header

item 0 item 1 item 2 item 3 item 4

item 5 item 6 item 7

section 0

section 1

Page 88: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping ItemsUsing sections

Header

Footer

Header

item 0 item 1 item 2 item 3 item 4

item 5 item 6 item 7

item 0 item 1 item 2 item 3 item 4

section 0

section 1

Page 89: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Referencing ItemsAllowing for sections

Page 90: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Referencing ItemsAllowing for sections

Page 91: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Referencing ItemsAllowing for sections

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Page 92: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Referencing ItemsAllowing for sections

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Page 93: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

itemAtIndexPath(NSIndexPath) -> NSCollectionViewItem?

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Referencing ItemsAllowing for sections

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Page 94: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

itemAtIndexPath(NSIndexPath) -> NSCollectionViewItem?

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Referencing ItemsAllowing for sections

itemAtIndexPath(NSIndexPath) -> NSCollectionViewItem?

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Page 95: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

itemAtIndexPath(NSIndexPath) -> NSCollectionViewItem?

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Referencing ItemsAllowing for sections

itemAtIndexPath(NSIndexPath) -> NSCollectionViewItem?

(section, item)

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Page 96: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

itemAtIndexPath(NSIndexPath) -> NSCollectionViewItem?

NSIndexPath

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Referencing ItemsAllowing for sections

itemAtIndexPath(NSIndexPath) -> NSCollectionViewItem?

(section, item)

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Page 97: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

itemAtIndexPath(NSIndexPath) -> NSCollectionViewItem?

NSIndexPath

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Referencing ItemsAllowing for sections

itemAtIndexPath(NSIndexPath) -> NSCollectionViewItem?

(section, item)

func itemAtIndex(index: Int) -> NSCollectionViewItem?

Page 98: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Nuts and BoltsPutting CollectionView to work

Page 99: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Example“Cocoa Slide Collection”

https://developer.apple.com/library/prerelease/mac/samplecode/CocoaSlideCollection

Page 100: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Example“Cocoa Slide Collection”

Browse a folder of image files

https://developer.apple.com/library/prerelease/mac/samplecode/CocoaSlideCollection

Page 101: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Example“Cocoa Slide Collection”

Browse a folder of image filesShow thumbnails and image info

https://developer.apple.com/library/prerelease/mac/samplecode/CocoaSlideCollection

Page 102: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Example“Cocoa Slide Collection”

Browse a folder of image filesShow thumbnails and image infoPosition using Flow and custom layouts

https://developer.apple.com/library/prerelease/mac/samplecode/CocoaSlideCollection

Page 103: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Example“Cocoa Slide Collection”

Browse a folder of image filesShow thumbnails and image infoPosition using Flow and custom layoutsGroup images by tag

https://developer.apple.com/library/prerelease/mac/samplecode/CocoaSlideCollection

Page 104: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Example“Cocoa Slide Collection”

Browse a folder of image filesShow thumbnails and image infoPosition using Flow and custom layoutsGroup images by tagSupport selection, Drag and Drop

https://developer.apple.com/library/prerelease/mac/samplecode/CocoaSlideCollection

Page 105: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Page 106: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appear

Page 107: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sections

Page 108: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changes

Page 109: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlighting

Page 110: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag and Drop

Page 111: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag and DropCustomize layout

Page 112: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag-and-DropCustomize Layout

Page 113: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Make Items Appear

dataSource

Data Source

delegate

Delegate

NSCollectionView

NSCollectionViewLayout

collectionViewLayout view

Slide.xib

NSCollectionViewItem

Page 114: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Make Items Appear

dataSource

Data Source

delegate

Delegate

NSCollectionView

NSCollectionViewLayout

collectionViewLayout view

Slide.xib

NSCollectionViewItem

Page 115: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Make Items Appear

dataSource

Data Source

delegate

Delegate

NSCollectionView

NSCollectionViewLayout

collectionViewLayout view

Slide.xib

NSCollectionViewItem

Page 116: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Make Items Appear

dataSource

Data Source

delegate

Delegate

NSCollectionView

NSCollectionViewLayout

collectionViewLayout view

Slide.xib

NSCollectionViewItem

Page 117: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewDataSourceTwo required methods

Page 118: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

NSCollectionViewDataSourceTwo required methods

func numberOfItemsInSection(Int) -> Int

func collectionView(NSCollectionView,

itemForRepresentedObjectAtIndexPath: NSIndexPath) ->

NSCollectionViewItem

Page 119: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Example“Cocoa Slide Collection”

Page 120: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Example“Cocoa Slide Collection”

ImageFile–our Model object

Page 121: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Example“Cocoa Slide Collection”

ImageFile–our Model object• url (includes filename)

Page 122: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Example“Cocoa Slide Collection”

ImageFile–our Model object• url (includes filename)• fileType (UTI)

Page 123: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Example“Cocoa Slide Collection”

ImageFile–our Model object• url (includes filename)• fileType (UTI)• pixelsWide

Page 124: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Example“Cocoa Slide Collection”

ImageFile–our Model object• url (includes filename)• fileType (UTI)• pixelsWide• pixelsHigh

Page 125: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Example“Cocoa Slide Collection”

ImageFile–our Model object• url (includes filename)• fileType (UTI)• pixelsWide• pixelsHigh• previewImage

Page 126: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

DemoMaking items appear

Page 127: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag-and-DropCustomize Layout

Page 128: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag-and-DropCustomize Layout

Page 129: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping Items into Sections

Page 130: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping Items into Sections

We’ll group our ImageFiles by tag

Page 131: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping Items into Sections

We’ll group our ImageFiles by tag• For each tag, we have an NSArray of ImageFiles

Page 132: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping Items into Sections

We’ll group our ImageFiles by tag• For each tag, we have an NSArray of ImageFiles• An ImageFile with many tags will appear many times

Page 133: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping Items into Sections

We’ll group our ImageFiles by tag• For each tag, we have an NSArray of ImageFiles• An ImageFile with many tags will appear many times

We’ll give each section a header and footer view

Page 134: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping Items into Sections

We’ll group our ImageFiles by tag• For each tag, we have an NSArray of ImageFiles• An ImageFile with many tags will appear many times

We’ll give each section a header and footer view• As with item types, we provide a nib or class

Page 135: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping Items into Sections

We’ll group our ImageFiles by tag• For each tag, we have an NSArray of ImageFiles• An ImageFile with many tags will appear many times

We’ll give each section a header and footer view• As with item types, we provide a nib or class• A header or footer is considered a “supplementary view”

Page 136: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Grouping Items into Sections

We’ll group our ImageFiles by tag• For each tag, we have an NSArray of ImageFiles• An ImageFile with many tags will appear many times

We’ll give each section a header and footer view• As with item types, we provide a nib or class• A header or footer is considered a “supplementary view”• Implement NSCollectionViewDataSource’scollectionView(_:viewForSupplementaryElementOfKind:indexPath:)

Page 137: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

DemoGrouping items into sections

Page 138: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag-and-DropCustomize Layout

Page 139: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag-and-DropCustomize Layout

Page 140: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Keeping Model and View in Sync

Page 141: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Keeping Model and View in Sync

Basic operations: Insert, delete, move, reload

Page 142: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Keeping Model and View in Sync

Basic operations: Insert, delete, move, reloadApplicable to both items and sections

Page 143: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Keeping Model and View in Sync

Basic operations: Insert, delete, move, reloadApplicable to both items and sectionsSimilar approach to View-based NSOutlineView

Page 144: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Keeping Model and View in Sync

Basic operations: Insert, delete, move, reloadApplicable to both items and sectionsSimilar approach to View-based NSOutlineView• When model changes, dataSource must notify CollectionView, describing the change

Page 145: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Keeping Model and View in Sync

Basic operations: Insert, delete, move, reloadApplicable to both items and sectionsSimilar approach to View-based NSOutlineView• When model changes, dataSource must notify CollectionView, describing the change

By default, changes appear instantly

To animate a change, message through the CollectionView’s animator (inherited from NSView)collectionView.animator.insertItemsAtIndexPaths(insertionIndexPaths)

collectionView.insertItemsAtIndexPaths(insertionIndexPaths)

Page 146: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

In CocoaSlideCollection

Page 147: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

In CocoaSlideCollection

Watch image folder for changes

Page 148: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

In CocoaSlideCollection

Watch image folder for changesUpdate our CollectionView to match

NSCollectionView

Page 149: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

In CocoaSlideCollection

Watch image folder for changesUpdate our CollectionView to matchImageFiles may be added, deleted, or changed

NSCollectionView

Page 150: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

In CocoaSlideCollection

Watch image folder for changesUpdate our CollectionView to matchImageFiles may be added, deleted, or changedKey-Value Observing (KVO) is our friend

NSCollectionView

Page 151: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

DemoKeeping model and view in sync

Page 152: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag-and-DropCustomize Layout

Page 153: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag-and-DropCustomize Layout

Page 154: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Selection and Highlighting

Page 155: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Selection and Highlighting

Visually indicated states

Page 156: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Selection and Highlighting

Visually indicated statesHighlighting precedes selection

Page 157: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Selection and Highlighting

Visually indicated statesHighlighting precedes selection

Page 158: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting

Page 159: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting

item.highlightState indicates proposed selection, deselection, or drop target status

Page 160: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting

item.highlightState indicates proposed selection, deselection, or drop target status

class NSCollectionViewItem { var highlightState: NSCollectionViewItemHighlightState

Page 161: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting

item.highlightState indicates proposed selection, deselection, or drop target status

enum NSCollectionViewItemHighlightState : Int {

case None, ForSelection, ForDeselection, AsDropTarget }

class NSCollectionViewItem { var highlightState: NSCollectionViewItemHighlightState

Page 162: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting

item.highlightState indicates proposed selection, deselection, or drop target status

enum NSCollectionViewItemHighlightState : Int {

case None, ForSelection, ForDeselection, AsDropTarget }

class NSCollectionViewItem { var highlightState: NSCollectionViewItemHighlightState

Page 163: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting

item.highlightState indicates proposed selection, deselection, or drop target status

enum NSCollectionViewItemHighlightState : Int {

case None, ForSelection, ForDeselection, AsDropTarget }

class NSCollectionViewItem { var highlightState: NSCollectionViewItemHighlightState

Page 164: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting

item.highlightState indicates proposed selection, deselection, or drop target status

enum NSCollectionViewItemHighlightState : Int {

case None, ForSelection, ForDeselection, AsDropTarget }

class NSCollectionViewItem { var highlightState: NSCollectionViewItemHighlightState

Page 165: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting

item.highlightState indicates proposed selection, deselection, or drop target status

enum NSCollectionViewItemHighlightState : Int {

case None, ForSelection, ForDeselection, AsDropTarget }

class NSCollectionViewItem { var highlightState: NSCollectionViewItemHighlightState

Page 166: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting

item.highlightState indicates proposed selection, deselection, or drop target status

enum NSCollectionViewItemHighlightState : Int {

case None, ForSelection, ForDeselection, AsDropTarget }

class NSCollectionViewItem { var highlightState: NSCollectionViewItemHighlightState

Page 167: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting Using Layer Properties

Page 168: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting Using Layer Properties

New NSCollectionViews always layer-backed

Page 169: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting Using Layer Properties

New NSCollectionViews always layer-backedCan use layer properties to easily change item appearance without redraw

Page 170: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting Using Layer Properties

New NSCollectionViews always layer-backedCan use layer properties to easily change item appearance without redraw• backgroundColor, borderColor, borderWidth, cornerRadius

Page 171: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting Using Layer Properties

New NSCollectionViews always layer-backedCan use layer properties to easily change item appearance without redraw• backgroundColor, borderColor, borderWidth, cornerRadius

Page 172: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting Using Layer Properties

New NSCollectionViews always layer-backedCan use layer properties to easily change item appearance without redraw• backgroundColor, borderColor, borderWidth, cornerRadius

item.view.layer.backgroundColor = brightColor

Page 173: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting Using Layer Properties

New NSCollectionViews always layer-backedCan use layer properties to easily change item appearance without redraw• backgroundColor, borderColor, borderWidth, cornerRadius

item.view.layer.backgroundColor = brightColoritem.view.layer.cornerRadius = 8

Page 174: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

When to Apply Highlighting

Page 175: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

When to Apply Highlighting

When “highlightState” changes

Page 176: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

When to Apply Highlighting

When “highlightState” changes

class MyItem : NSCollectionViewItem { override var highlightState: NSCollectionViewItemHighlightState { didSet { // Update this item’s appearance accordingly. } } }

Page 177: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Selection

Page 178: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Selection

Items can be selected

Page 179: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Selection

Items can be selectedNSCollectionView supports single or multiple selection

Page 180: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Selection

Items can be selectedNSCollectionView supports single or multiple selection

var selectable: Bool

Page 181: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Selection

Items can be selectedNSCollectionView supports single or multiple selection

var selectable: Bool

var allowsMultipleSelection: Bool

Page 182: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Selection

Items can be selectedNSCollectionView supports single or multiple selection

var allowsMultipleSelection: Bool

var selectable: Bool

var allowsEmptySelection: Bool

Page 183: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Tracking the Selection

Page 184: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Tracking the Selection

selectionIndexPaths tracks the set of selected items

Page 185: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Tracking the Selection

selectionIndexPaths tracks the set of selected items

var selectionIndexPaths: Set<NSIndexPath>

Page 186: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Tracking the Selection

selectionIndexPaths tracks the set of selected items

var selectionIndexPaths: Set<NSIndexPath>

An item knows whether it is part of the selection

Page 187: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Tracking the Selection

selectionIndexPaths tracks the set of selected items

var selectionIndexPaths: Set<NSIndexPath>

An item knows whether it is part of the selection

var selected: Bool

Page 188: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Programmatic Selection

var selectionIndexPaths: Set<NSIndexPath>

Page 189: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Programmatic Selection

var selectionIndexPaths: Set<NSIndexPath>

Page 190: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Programmatic Selection

var selectionIndexPaths: Set<NSIndexPath>

func selectItemsAtIndexPaths(Set<NSIndexPath>, scrollPosition: NSCollectionViewScrollPosition)

func deselectItemsAtIndexPaths(Set<NSIndexPath>)

var selectionIndexPaths: Set<NSIndexPath>

Page 191: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

User Selection

Page 192: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

User Selection

Delegate can approve selection and deselection

Page 193: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

User Selection

Delegate can approve selection and deselection

optional func collectionView(NSCollectionView, shouldSelectItemsAtIndexPaths: Set<NSIndexPath>) -> Set<NSIndexPath>

optional func collectionView(NSCollectionView, shouldDeselectItemsAtIndexPaths: Set<NSIndexPath>) -> Set<NSIndexPath>

Page 194: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

User Selection

Delegate can approve selection and deselectionReturn a different set of NSIndexPaths to override the proposed change

optional func collectionView(NSCollectionView, shouldSelectItemsAtIndexPaths: Set<NSIndexPath>) -> Set<NSIndexPath>

optional func collectionView(NSCollectionView, shouldDeselectItemsAtIndexPaths: Set<NSIndexPath>) -> Set<NSIndexPath>

Page 195: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

User Selection

Delegate is notified of selection and deselection

optional func collectionView(NSCollectionView, didSelectItemsAtIndexPaths: Set<NSIndexPath>) optional func collectionView(NSCollectionView, didDeselectItemsAtIndexPaths: Set<NSIndexPath>)

Page 196: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting

Page 197: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting

Delegate can also approve highlightState changes

Page 198: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Highlighting

Delegate can also approve highlightState changes

optional func collectionView(NSCollectionView, shouldChangeItemsAtIndexPaths: Set<NSIndexPath>, toHighlightState: NSCollectionViewItemHighlightState) -> Set<NSIndexPath>

optional func collectionView(NSCollectionView, didChangeItemsAtIndexPaths: Set<NSIndexPath>, toHighlightState: NSCollectionViewItemHighlightState)

Page 199: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

DemoSelection and highlighting

Page 200: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag-and-DropCustomize Layout

Page 201: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag and DropCustomize Layout

Page 202: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Handling Drag and Drop

Page 203: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Handling Drag and Drop

Page 204: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Handling Drag and Drop

Page 205: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Handling Drag and Drop

Page 206: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Handling Drag and Drop

NSCollectionView’s delegate implements Drag-and-Drop response

Page 207: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Handling Drag and Drop

NSCollectionView’s delegate implements Drag-and-Drop responseSimilar to NSOutlineView’s API (c.f., “DragAndDropOutlineView”)

Page 208: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Handling Drag and Drop

NSCollectionView’s delegate implements Drag-and-Drop responseSimilar to NSOutlineView’s API (c.f., “DragAndDropOutlineView”)Dragging Source

Page 209: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Handling Drag and Drop

NSCollectionView’s delegate implements Drag-and-Drop responseSimilar to NSOutlineView’s API (c.f., “DragAndDropOutlineView”)Dragging Source• Put items on pasteboard when requested

Page 210: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Handling Drag and Drop

NSCollectionView’s delegate implements Drag-and-Drop responseSimilar to NSOutlineView’s API (c.f., “DragAndDropOutlineView”)Dragging Source• Put items on pasteboard when requested

Dragging Destination

Page 211: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Handling Drag and Drop

NSCollectionView’s delegate implements Drag-and-Drop responseSimilar to NSOutlineView’s API (c.f., “DragAndDropOutlineView”)Dragging Source• Put items on pasteboard when requested

Dragging Destination• Assess proposed drop objects, target position, and operation; optionally override

Page 212: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Handling Drag and Drop

NSCollectionView’s delegate implements Drag-and-Drop responseSimilar to NSOutlineView’s API (c.f., “DragAndDropOutlineView”)Dragging Source• Put items on pasteboard when requested

Dragging Destination• Assess proposed drop objects, target position, and operation; optionally override• Implement drop acceptance

Page 213: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Requirements

Page 214: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Requirements

registerForDraggedTypes(_:)

Page 215: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Requirements

registerForDraggedTypes(_:)

setDraggingSourceOperationMask(_:forLocal:)

Page 216: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Requirements

registerForDraggedTypes(_:)

setDraggingSourceOperationMask(_:forLocal:)

Implement the required delegate methods

Page 217: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Requirements

registerForDraggedTypes(_:)

setDraggingSourceOperationMask(_:forLocal:)

Implement the required delegate methods• Source

Page 218: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Requirements

registerForDraggedTypes(_:)

setDraggingSourceOperationMask(_:forLocal:)

Implement the required delegate methods• Source

- collectionView(_:pasteboardWriterForItemAtIndexPath:), or

Page 219: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Requirements

registerForDraggedTypes(_:)

setDraggingSourceOperationMask(_:forLocal:)

Implement the required delegate methods• Source

- collectionView(_:pasteboardWriterForItemAtIndexPath:), or- collectionView(_:writeItemsAtIndexPaths:toPasteboard:)

Page 220: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Requirements

registerForDraggedTypes(_:)

setDraggingSourceOperationMask(_:forLocal:)

Implement the required delegate methods• Source

- collectionView(_:pasteboardWriterForItemAtIndexPath:), or- collectionView(_:writeItemsAtIndexPaths:toPasteboard:)

• Destination

Page 221: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Requirements

registerForDraggedTypes(_:)

setDraggingSourceOperationMask(_:forLocal:)

Implement the required delegate methods• Source

- collectionView(_:pasteboardWriterForItemAtIndexPath:), or- collectionView(_:writeItemsAtIndexPaths:toPasteboard:)

• Destination- collectionView(_:validateDrop:proposedIndexPath:dropOperation:)

Page 222: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Requirements

registerForDraggedTypes(_:)

setDraggingSourceOperationMask(_:forLocal:)

Implement the required delegate methods• Source

- collectionView(_:pasteboardWriterForItemAtIndexPath:), or- collectionView(_:writeItemsAtIndexPaths:toPasteboard:)

• Destination- collectionView(_:validateDrop:proposedIndexPath:dropOperation:)- collectionView(_:acceptDrop:indexPath:dropOperation:)

Page 223: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Tips

Page 224: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Tips

It’s worth handling drag within your CollectionView specially

Page 225: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Tips

It’s worth handling drag within your CollectionView speciallyThis lets you move items, rather than remove and reinsert

Page 226: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Tips

It’s worth handling drag within your CollectionView speciallyThis lets you move items, rather than remove and reinsertStash indexPaths being dragged to know drag came from same CollectionView

Page 227: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Drag-and-Drop Tips

It’s worth handling drag within your CollectionView speciallyThis lets you move items, rather than remove and reinsertStash indexPaths being dragged to know drag came from same CollectionView

collectionView(_:draggingSession:willBeginAtPoint:forItemsAtIndexPaths:)

Page 228: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag and DropCustomize Layout

Page 229: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag-and-DropCustomize Layout

Page 230: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Customizing Layout

Page 231: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Customizing Layout

Adjust an existing layout

Page 232: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Customizing Layout

Adjust an existing layoutImplement a completely new layout

Page 233: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Adjust an Existing Layout

Page 234: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Adjust an Existing Layout

Subclass NSCollectionViewFlowLayout to adjust item positioning

Page 235: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Adjust an Existing Layout

Subclass NSCollectionViewFlowLayout to adjust item positioningOverride

Page 236: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Adjust an Existing Layout

Subclass NSCollectionViewFlowLayout to adjust item positioningOverride• func layoutAttributesForElementsInRect(rect: NSRect) -> [NSCollectionViewLayoutAttributes]

Page 237: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Adjust an Existing Layout

Subclass NSCollectionViewFlowLayout to adjust item positioningOverride• func layoutAttributesForElementsInRect(rect: NSRect) -> [NSCollectionViewLayoutAttributes]

• func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> NSCollectionViewLayoutAttributes?

Page 238: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Adjust an Existing Layout

Subclass NSCollectionViewFlowLayout to adjust item positioningOverride• func layoutAttributesForElementsInRect(rect: NSRect) -> [NSCollectionViewLayoutAttributes]

• func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> NSCollectionViewLayoutAttributes?

• func invalidateLayoutWithContext(context: NSCollectionViewLayoutInvalidationContext)

Page 239: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Adjust an Existing Layout

Subclass NSCollectionViewFlowLayout to adjust item positioningOverride• func layoutAttributesForElementsInRect(rect: NSRect) -> [NSCollectionViewLayoutAttributes]

• func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> NSCollectionViewLayoutAttributes?

• func invalidateLayoutWithContext(context: NSCollectionViewLayoutInvalidationContext)

Examine and adjust super-proposed item frames

Page 240: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Completely Custom Layouts

Page 241: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Completely Custom Layouts

Can subclass NSCollectionViewLayout directly

Page 242: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Completely Custom Layouts

Can subclass NSCollectionViewLayout directlyImplement same methods as for customizing Flow, plus

Page 243: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Completely Custom Layouts

Can subclass NSCollectionViewLayout directlyImplement same methods as for customizing Flow, plus• func collectionViewContentSize() -> NSSize

Page 244: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Completely Custom Layouts

Can subclass NSCollectionViewLayout directlyImplement same methods as for customizing Flow, plus• func collectionViewContentSize() -> NSSize

• func shouldInvalidateLayoutForBoundsChange(newBounds: NSRect) -> Bool

Page 245: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Hit-Testing for a Drop Target

Page 246: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Hit-Testing for a Drop Target

func layoutAttributesForDropTargetAtPoint(pointInCollectionView: NSPoint) -> NSCollectionViewLayoutAttributes?

Page 247: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Target is an item

Hit-Testing for a Drop Target

func layoutAttributesForDropTargetAtPoint(pointInCollectionView: NSPoint) -> NSCollectionViewLayoutAttributes?

Page 248: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Target is an item

Hit-Testing for a Drop Target

func layoutAttributesForDropTargetAtPoint(pointInCollectionView: NSPoint) -> NSCollectionViewLayoutAttributes?

attributes.representedElementCategory = .Item

Page 249: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Target is an item

Hit-Testing for a Drop Target

func layoutAttributesForDropTargetAtPoint(pointInCollectionView: NSPoint) -> NSCollectionViewLayoutAttributes?

attributes.representedElementCategory = .Item

attributes.indexPath = /* NSIndexPath of the item we’re dropping onto */

Page 250: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Target is an item

Hit-Testing for a Drop Target

func layoutAttributesForDropTargetAtPoint(pointInCollectionView: NSPoint) -> NSCollectionViewLayoutAttributes?

attributes.representedElementCategory = .Item

attributes.indexPath = /* NSIndexPath of the item we’re dropping onto */

attributes.frame = /* NSRect item frame */

Page 251: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Target is a gap between items

Target is an item

Hit-Testing for a Drop Target

func layoutAttributesForDropTargetAtPoint(pointInCollectionView: NSPoint) -> NSCollectionViewLayoutAttributes?

attributes.representedElementCategory = .Item

attributes.indexPath = /* NSIndexPath of the item we’re dropping onto */

attributes.frame = /* NSRect item frame */

Page 252: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Target is a gap between items

Target is an item

Hit-Testing for a Drop Target

func layoutAttributesForDropTargetAtPoint(pointInCollectionView: NSPoint) -> NSCollectionViewLayoutAttributes?

attributes.representedElementCategory = .InterItemGap

attributes.representedElementCategory = .Item

attributes.indexPath = /* NSIndexPath of the item we’re dropping onto */

attributes.frame = /* NSRect item frame */

Page 253: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Target is a gap between items

Target is an item

Hit-Testing for a Drop Target

func layoutAttributesForDropTargetAtPoint(pointInCollectionView: NSPoint) -> NSCollectionViewLayoutAttributes?

attributes.representedElementCategory = .InterItemGap

attributes.indexPath = /* NSIndexPath of the item we’re dropping before */

attributes.representedElementCategory = .Item

attributes.indexPath = /* NSIndexPath of the item we’re dropping onto */

attributes.frame = /* NSRect item frame */

Page 254: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Target is a gap between items

Target is an item

Hit-Testing for a Drop Target

func layoutAttributesForDropTargetAtPoint(pointInCollectionView: NSPoint) -> NSCollectionViewLayoutAttributes?

attributes.representedElementCategory = .InterItemGap

attributes.indexPath = /* NSIndexPath of the item we’re dropping before */

attributes.frame = /* NSRect gap frame */

attributes.representedElementCategory = .Item

attributes.indexPath = /* NSIndexPath of the item we’re dropping onto */

attributes.frame = /* NSRect item frame */

Page 255: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Finding Inter-Item Gaps by NSIndexPath

Page 256: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Finding Inter-Item Gaps by NSIndexPath

func layoutAttributesForInterItemGapBeforeIndexPath(indexPath: NSIndexPath) -> NSCollectionViewLayoutAttributes?

Page 257: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Result

Finding Inter-Item Gaps by NSIndexPath

func layoutAttributesForInterItemGapBeforeIndexPath(indexPath: NSIndexPath) -> NSCollectionViewLayoutAttributes?

Page 258: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Result

Finding Inter-Item Gaps by NSIndexPath

func layoutAttributesForInterItemGapBeforeIndexPath(indexPath: NSIndexPath) -> NSCollectionViewLayoutAttributes?

attributes.representedElementCategory = .InterItemGap

Page 259: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Result

Finding Inter-Item Gaps by NSIndexPath

func layoutAttributesForInterItemGapBeforeIndexPath(indexPath: NSIndexPath) -> NSCollectionViewLayoutAttributes?

attributes.representedElementCategory = .InterItemGap

attributes.representedElementKind = .InterItemGapIndicator

Page 260: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Result

Finding Inter-Item Gaps by NSIndexPath

func layoutAttributesForInterItemGapBeforeIndexPath(indexPath: NSIndexPath) -> NSCollectionViewLayoutAttributes?

attributes.representedElementCategory = .InterItemGap

attributes.representedElementKind = .InterItemGapIndicator

attributes.indexPath = /* NSIndexPath of the item gap comes before */

Page 261: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Result

Finding Inter-Item Gaps by NSIndexPath

func layoutAttributesForInterItemGapBeforeIndexPath(indexPath: NSIndexPath) -> NSCollectionViewLayoutAttributes?

attributes.representedElementCategory = .InterItemGap

attributes.representedElementKind = .InterItemGapIndicator

attributes.indexPath = /* NSIndexPath of the item gap comes before */

attributes.frame = /* NSRect gap frame */

Page 262: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

DemoCustom item layouts

Page 263: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag-and-DropCustomize Layout

Page 264: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Roadmap

Make items appearGroup items into sectionsUpdate when model changesHandle selection and highlightingHandle Drag-and-DropCustomize Layout

Page 265: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Conclusion

Page 266: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

New NSCollectionView is Ready!

…to handle your toughest projects!

Page 267: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Coming Up

Need help or guidance? Come to our lab!

Cocoa and NSCollectionView Lab Frameworks Lab B Friday 9:00AM

Page 268: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

More Information

Documentationhttp://developer.apple.com/library/mac

Technical SupportApp Developer Forumshttp://developer.apple.com/forums

General InquiriesPaul Marcos, App Frameworks [email protected]

Page 269: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports

Related Sessions

What’s New in Cocoa Presidio Tuesday 1:30PM

Mysteries of Auto Layout, Part 1 Presidio Thursday 11:00AM

Mysteries of Auto Layout, Part 2 Presidio Thursday 1:30PM

Page 270: What’s New in NSCollectionView - Apple Inc....“Stretchy” grid Items all same size Like flowing text fragments/CSS boxes Variable item size No sections/headers/footers Supports