Mac Developer Library

Developer

AppKit Framework Reference NSCollectionView Class Reference

Options
Deployment Target:

On This Page
Language:

NSCollectionView

NSCollectionView class displays an array of content as a grid of views. The views are specified using the NSCollectionViewItem class which makes loadings nibs containing the view easy, and supports bindings.

Inheritance


Import Statement


import AppKit @import AppKit;

Availability


Available in OS X v10.5 and later.
  • Sets the receiver’s item prototype to the specified collection view item.

    Declaration

    Swift

    var itemPrototype: NSCollectionViewItem!

    Objective-C

    @property(strong) NSCollectionViewItem *itemPrototype

    Parameters

    prototype

    The collection view item used as the prototype by the receiver.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Returns the receiver’s collection view item prototype.

    Declaration

    Swift

    var itemPrototype: NSCollectionViewItem!

    Objective-C

    @property(strong) NSCollectionViewItem *itemPrototype

    Return Value

    The receiver’s collection view item prototype.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Returns the collection view item that is used for the specified object.

    Declaration

    Swift

    func newItemForRepresentedObject(_ object: AnyObject!) -> NSCollectionViewItem!

    Objective-C

    - (NSCollectionViewItem *)newItemForRepresentedObject:(id)object

    Parameters

    object

    The content object that the collection view item will represent.

    Return Value

    An initialized collection view item with the specified object and the appropriate view set. The collection view item should not be autoreleased.

    Discussion

    Subclasses can override this method if the collection view items are not generated from a prototype or if the prototype view needs to be modified. The subclass is responsible for setting the view and representedObject of the new collection view item.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • isFirstResponder isFirstResponder Available in OS X v10.5 through OS X v10.9

    Returns whether the receiver is the first responder.

    Declaration

    Objective-C

    - (BOOL)isFirstResponder

    Return Value

    YEStrue if the receiver is the first responder, otherwise NOfalse.

    Special Considerations

    This method is fully key-value observing compliant.

    Import Statement

    Availability

    Available in OS X v10.5 through OS X v10.9.

  • Sets the receiver’s content array.

    Declaration

    Swift

    var content: [AnyObject]!

    Objective-C

    @property(copy) NSArray *content

    Parameters

    content

    An array containing the receiver’s content.

    Discussion

    The content array can also be provided by creating a binding between the receiver’s NSContentBinding and an array controller’s arrangedObjects method.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Returns the receiver’s content object.

    Declaration

    Swift

    var content: [AnyObject]!

    Objective-C

    @property(copy) NSArray *content

    Return Value

    An array containing the receiver’s content.

    Discussion

    This property is observable using key-value observing.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Controls whether the receiver allows the user to select items.

    Declaration

    Swift

    var selectable: Bool

    Objective-C

    @property(getter=isSelectable) BOOL selectable

    Parameters

    flag

    If flag is YEStrue, the receiver allows the user to select items; if flag is NOfalse, it doesn’t.

    Discussion

    You can set selections programmatically regardless of this setting.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • isSelectable isSelectable Available in OS X v10.5 through OS X v10.9

    Returns a Boolean value that indicates whether the receiver allows the user to select items, NOfalse if it doesn’t.

    Declaration

    Objective-C

    - (BOOL)isSelectable

    Return Value

    YEStrue if the receiver allows the user to select items, otherwise NOfalse.

    Import Statement

    Availability

    Available in OS X v10.5 through OS X v10.9.

  • Controls whether the user can select multiple items at a time.

    Declaration

    Swift

    var allowsMultipleSelection: Bool

    Objective-C

    @property BOOL allowsMultipleSelection

    Parameters

    flag

    YEStrue to allow the user to select multiple items, otherwise NOfalse.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Returns a Boolean value that indicates whether the receiver allows the user to select more than one item at a time.

    Declaration

    Swift

    var allowsMultipleSelection: Bool

    Objective-C

    @property BOOL allowsMultipleSelection

    Return Value

    YEStrue if the receiver allows the user to select more than one column or row at a time, otherwise NOfalse.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Sets the receiver’s selection using the specified indexes.

    Declaration

    Swift

    @NSCopying var selectionIndexes: NSIndexSet!

    Objective-C

    @property(copy) NSIndexSet *selectionIndexes

    Parameters

    indexes

    The set of selection indexes for the receiver.

    Discussion

    To select all the receiver’s objects, indexes should be an index set with indexes [0...count -1]. To deselect all indexes, pass an empty index set.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Returns an index set containing the indexes of the receiver’s currently selected objects in the content array.

    Declaration

    Swift

    @NSCopying var selectionIndexes: NSIndexSet!

    Objective-C

    @property(copy) NSIndexSet *selectionIndexes

    Return Value

    An index set containing the indexes of the receiver’s currently selected objects in the content array.

    Discussion

    This property is observable using key-value observing.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Sets the maximum number of rows the receiver will display.

    Declaration

    Swift

    var maxNumberOfRows: Int

    Objective-C

    @property NSUInteger maxNumberOfRows

    Parameters

    number

    The maximum number of rows the receiver can display.

    Discussion

    Setting to 0 specifies no maximum number of rows. Defaults to 0.

    It is possible for a NSCollectionView instance to specify both the maximum number of rows and a maximum number of columns. If the number of content objects exceeds the number of displayable items (n=maxNumberOfRows * maxNumberOfColumns) only the first n items of the content array are displayed.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Returns the maximum number of rows the receiver will display.

    Declaration

    Swift

    var maxNumberOfRows: Int

    Objective-C

    @property NSUInteger maxNumberOfRows

    Return Value

    The maximum number of rows the receiver will display.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Sets the maximum number of columns the receiver will display

    Declaration

    Swift

    var maxNumberOfColumns: Int

    Objective-C

    @property NSUInteger maxNumberOfColumns

    Parameters

    number

    The maximum number of columns the receiver will display.

    Discussion

    Setting to 0 specifies no maximum number of columns. Defaults to 0.

    It is possible for a NSCollectionView instance to specify both the maximum number of rows and a maximum number of columns. If the number of content objects exceeds the number of displayable items (n=maxNumberOfRows * maxNumberOfColumns) only the first n items of the content array are displayed.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Returns the maximum number of columns the receiver will display.

    Declaration

    Swift

    var maxNumberOfColumns: Int

    Objective-C

    @property NSUInteger maxNumberOfColumns

    Return Value

    The maximum number of columns the receiver will display.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Sets the minimum size used to display individual layout items in the grid.

    Declaration

    Swift

    var minItemSize: NSSize

    Objective-C

    @property NSSize minItemSize

    Parameters

    size

    The new minimum size, measured in points, with which to display individual layout items.

    Discussion

    The default is (0.0). If the view in the receiver’s collection view item prototype is resizable you should set this to the minimum size that the view should be displayed using.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Returns the minimum size used to display individual collection view items in the grid.

    Declaration

    Swift

    var minItemSize: NSSize

    Objective-C

    @property NSSize minItemSize

    Return Value

    The minimum size, measured in points, used to display individual collection view items.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Sets the maximum size used to display individual collection view items in the grid.

    Declaration

    Swift

    var maxItemSize: NSSize

    Objective-C

    @property NSSize maxItemSize

    Parameters

    size

    The new maximum size, measured in points, with which to display individual collection view items.

    Discussion

    Setting the size to (0,0) specifies no maximum grid size. The default is (0.0). If the view in the receiver’s collection view item prototype is resizable you should set this to the maximum size that the view should be displayed using.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Returns the maximum size used to display individual collection view items in the grid

    Declaration

    Swift

    var maxItemSize: NSSize

    Objective-C

    @property NSSize maxItemSize

    Return Value

    The maximum size, measured in points, used to display individual collection view items.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Sets the receiver's background colors to the specified array of colors.

    Declaration

    Swift

    var backgroundColors: [AnyObject]!

    Objective-C

    @property(copy) NSArray *backgroundColors

    Parameters

    colors

    An array containing the background colors for the receiver.

    Discussion

    Passing an empty array or nil resets the background colors to their default values provided by controlAlternatingRowBackgroundColors.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Return the receiver’s background colors.

    Declaration

    Swift

    var backgroundColors: [AnyObject]!

    Objective-C

    @property(copy) NSArray *backgroundColors

    Return Value

    Returns an array containing the receiver’s background colors.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Returns the receiver’s delegate.

    Declaration

    Swift

    unowned(unsafe) var delegate: NSCollectionViewDelegate?

    Objective-C

    @property(assign) id<NSCollectionViewDelegate> delegate

    Return Value

    The receiver’s delegate object.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.6 and later.

  • Sets the receiver’s delegate.

    Declaration

    Swift

    unowned(unsafe) var delegate: NSCollectionViewDelegate?

    Objective-C

    @property(assign) id<NSCollectionViewDelegate> delegate

    Parameters

    aDelegate

    The delegate object for the receiver. The delegate must conform to the NSCollectionViewDelegate Protocol protocol.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.6 and later.

  • This method computes and returns an image to use for dragging.

    Declaration

    Swift

    func draggingImageForItemsAtIndexes(_ indexes: NSIndexSet!, withEvent event: NSEvent!, offset dragImageOffset: NSPointPointer) -> NSImage!

    Objective-C

    - (NSImage *)draggingImageForItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent *)event offset:(NSPointPointer)dragImageOffset

    Parameters

    indexes

    The index set of the items to be dragged.

    event

    Mouse drag event.

    dragImageOffset

    An in/out parameter that will initially be set to NSZeroPoint. it can be modified to reposition the returned image. A dragImageOffset of NSZeroPoint will cause the image to be centered under the mouse.

    Return Value

    An image containing a rendering of the visible portions of the views for each item.

    Discussion

    You can override the default image by subclassing NSCollectionView and overriding this method, or by implementing the collectionView:draggingImageForItemsAtIndexes:withEvent:offset: delegate method, it will be preferred over this method.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.6 and later.

  • Configures the default value returned from draggingSourceOperationMaskForLocal:.

    Declaration

    Swift

    func setDraggingSourceOperationMask(_ dragOperationMask: NSDragOperation, forLocal localDestination: Bool)

    Objective-C

    - (void)setDraggingSourceOperationMask:(NSDragOperation)dragOperationMask forLocal:(BOOL)localDestination

    Parameters

    dragOperationMask

    The types of drag operations allowed.

    localDestination

    If YEStrue, mask applies when the drag destination object is in the same application as the receiver; if NOfalse, mask applies when the destination object is outside the receiver’s application.

    Discussion

    By default, this method returns NSDragOperationEvery when localDestination is YEStrue and NSDragOperationNone when localDestination is NOfalse. NSCollectionView will save the values you set for each localDestination value.

    You typically will invoke this method, and not override it.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.6 and later.

  • Returns the collection view item for the represented object at the specified index.

    Declaration

    Swift

    func itemAtIndex(_ index: Int) -> NSCollectionViewItem!

    Objective-C

    - (NSCollectionViewItem *)itemAtIndex:(NSUInteger)index

    Parameters

    index

    The index of the collection view item.

    Return Value

    An instance of NSCollectionViewItem.

    Discussion

    Rather than using the NSCollectionViewItem instance returned by this method to determine the frame of the collection item’s view you should use frameForItemAtIndex:, it is significantly more efficient.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.6 and later.

  • Returns the frame of the collection view item at the specified index.

    Declaration

    Swift

    func frameForItemAtIndex(_ index: Int) -> NSRect

    Objective-C

    - (NSRect)frameForItemAtIndex:(NSUInteger)index

    Parameters

    index

    The index of the collection view item.

    Return Value

    Returns the frame calculated by the receiver where it intends to place the subview for the NSCollectionViewItem at the given index. The rectangle is returned in the receiver’s coordinate system.

    Discussion

    You can use this method in the collectionView:draggingImageForItemsAtIndexes:withEvent:offset: method to determine which views are in the visible portion of the enclosing scroll view.

    Overriding this method will have no effect on the receiver's subview layout.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.6 and later.

  • These constants specify if acceptance of a drop should be at the item it is dropped on or before the item. These constants are used by the collectionView:acceptDrop:index:dropOperation: and collectionView:validateDrop:proposedIndex:dropOperation: methods in NSCollectionViewDelegate Protocol

    Declaration

    Swift

    enum NSCollectionViewDropOperation : Int { case On case Before }

    Objective-C

    enum { NSCollectionViewDropOn = 0, NSCollectionViewDropBefore = 1, }; typedef NSInteger NSCollectionViewDropOperation;

    Constants

    • On

      NSCollectionViewDropOn

      The drop occurs at the collection view item to which the item was dragged.

      Available in OS X v10.6 and later.

    • Before

      NSCollectionViewDropBefore

      The drop occurs above the collection view item to which the item was dragged..

      Available in OS X v10.6 and later.

    Import Statement

    import AppKit

    Availability

    Available in OS X v10.6 and later.