Class: UICollectionView
- Inherits:
-
UIScrollView
- Object
- NSObject
- UIResponder
- UIView
- UIScrollView
- UICollectionView
Overview
The UICollectionView class manages an ordered collection of data items and presents them using customizable layouts. Collection views provide the same general function as table views except that a collection view is able to support more than just single-column layouts. Collection views support customizable layouts that can be used to implement multi-column grids, tiled layouts, circular layouts, and many more. You can even change the layout of a collection view dynamically if you want.
Instance Attribute Summary (collapse)
-
- allowsMultipleSelection
A Boolean value that determines whether users can select more than one item in the collection view.
-
- allowsSelection
A Boolean value that indicates whether users can select items in the collection view.
-
- backgroundView
The view that provides the background appearance.
-
- collectionViewLayout
The layout used to organize the collected view’s items.
-
- dataSource
The object that provides the data for the collection view.
-
- delegate
The object that acts as the delegate of the collection view.
Attributes inherited from UIScrollView
#alwaysBounceHorizontal, #alwaysBounceVertical, #bounces, #bouncesZoom, #canCancelContentTouches, #contentInset, #contentOffset, #contentSize, #decelerating, #decelerationRate, #delaysContentTouches, #directionalLockEnabled, #dragging, #indicatorStyle, #maximumZoomScale, #minimumZoomScale, #pagingEnabled, #panGestureRecognizer, #pinchGestureRecognizer, #scrollEnabled, #scrollIndicatorInsets, #scrollsToTop, #showsHorizontalScrollIndicator, #showsVerticalScrollIndicator, #tracking, #zoomBouncing, #zoomScale, #zooming
Attributes inherited from UIView
#alpha, #autoresizesSubviews, #autoresizingMask, #backgroundColor, #bounds, #center, #clearsContextBeforeDrawing, #clipsToBounds, #contentMode, #contentScaleFactor, #exclusiveTouch, #frame, #gestureRecognizers, #hidden, #layer, #multipleTouchEnabled, #opaque, #restorationIdentifier, #subviews, #superview, #tag, #transform, #userInteractionEnabled, #window
Attributes inherited from UIResponder
#inputAccessoryView, #inputView, #undoManager
Instance Method Summary (collapse)
-
- cellForItemAtIndexPath:
Returns the cell object at the specified index path.
-
- deleteItemsAtIndexPaths:
Deletes the items at the specified index paths.
-
- deleteSections:
Deletes the sections at the specified indexes.
-
- dequeueReusableCellWithReuseIdentifier:forIndexPath:
Returns a reusable cell object located by its identifier Call this method from your data source object when asked to provide a new cell for the collection view.
-
- dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
Returns a reusable supplementary view located by its identifier and kind.
-
- deselectItemAtIndexPath:animated:
Deselects the item at the specified index.
-
- indexPathForCell:
Returns the index path of the specified cell.
-
- indexPathForItemAtPoint:
Returns the index path of the item at the specified point in the collection view.
-
- indexPathsForSelectedItems
Returns the index paths for the selected items.
-
- indexPathsForVisibleItems
Returns an array of the visible items in the collection view.
-
- initWithFrame:collectionViewLayout:
Initializes and returns a newly allocated collection view object with the specified frame and layout.
-
- insertItemsAtIndexPaths:
Inserts new items at the specified index paths.
-
- insertSections:
Inserts new sections at the specified indexes.
-
- layoutAttributesForItemAtIndexPath:
Returns the layout information for the item at the specified index path.
-
- layoutAttributesForSupplementaryElementOfKind:atIndexPath:
Returns the layout information for the specified supplementary view.
-
- moveItemAtIndexPath:toIndexPath:
Moves an item from one location to another in the collection view.
-
- moveSection:toSection:
Moves a section from one location to another in the collection view.
-
- numberOfItemsInSection:
Returns the number of items in the specified section.
-
- numberOfSections
Returns the number of sections displayed by the collection view.
-
- performBatchUpdates:completion:
Animates multiple insert, delete, reload, and move operations as a group.
-
- registerClass:forCellWithReuseIdentifier:
Register a class for use in creating new collection view cells.
-
- registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
Registers a class for use in creating supplementary views for the collection view.
-
- registerNib:forCellWithReuseIdentifier:
Register a nib file for use in creating new collection view cells.
-
- registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
Registers a nib file for use in creating supplementary views for the collection view.
-
- reloadData
Reloads all of the data for the collection view.
-
- reloadItemsAtIndexPaths:
Reloads just the items at the specified index paths.
-
- reloadSections:
Reloads the data in the specified sections of the collection view.
-
- scrollToItemAtIndexPath:atScrollPosition:animated:
Scrolls the collection view contents until the specified item is visible.
-
- selectItemAtIndexPath:animated:scrollPosition:
Selects the item at the specified index path and optionally scrolls it into view.
-
- setCollectionViewLayout:animated:
Assigns a new layout object to the collection view and optionally animates the change.
-
- visibleCells
Returns an array of visible cells currently displayed by the collection view.
Methods inherited from UIScrollView
#flashScrollIndicators, #scrollRectToVisible:animated:, #setContentOffset:animated:, #setZoomScale:animated:, #touchesShouldBegin:withEvent:inContentView:, #touchesShouldCancelInContentView:, #zoomToRect:animated:
Methods inherited from UIView
#addConstraint:, #addConstraints:, #addGestureRecognizer:, #addSubview:, #alignmentRectForFrame:, #alignmentRectInsets, animateWithDuration:animations:, animateWithDuration:animations:completion:, animateWithDuration:delay:options:animations:completion:, areAnimationsEnabled, beginAnimations:context:, #bringSubviewToFront:, commitAnimations, #constraints, #constraintsAffectingLayoutForAxis:, #contentCompressionResistancePriorityForAxis:, #contentHuggingPriorityForAxis:, #convertPoint:fromView:, #convertPoint:toView:, #convertRect:fromView:, #convertRect:toView:, #decodeRestorableStateWithCoder:, #didAddSubview:, #didMoveToSuperview, #didMoveToWindow, #drawRect:, #drawRect:forViewPrintFormatter:, #encodeRestorableStateWithCoder:, #endEditing:, #exchangeSubviewAtIndex:withSubviewAtIndex:, #exerciseAmbiguityInLayout, #frameForAlignmentRect:, #gestureRecognizerShouldBegin:, #hasAmbiguousLayout, #hitTest:withEvent:, #initWithFrame:, #insertSubview:aboveSubview:, #insertSubview:atIndex:, #insertSubview:belowSubview:, #intrinsicContentSize, #invalidateIntrinsicContentSize, #isDescendantOfView:, layerClass, #layoutIfNeeded, #layoutSubviews, #needsUpdateConstraints, #pointInside:withEvent:, #removeConstraint:, #removeConstraints:, #removeFromSuperview, #removeGestureRecognizer:, requiresConstraintBasedLayout, #sendSubviewToBack:, setAnimationBeginsFromCurrentState:, setAnimationCurve:, setAnimationDelay:, setAnimationDelegate:, setAnimationDidStopSelector:, setAnimationDuration:, setAnimationRepeatAutoreverses:, setAnimationRepeatCount:, setAnimationStartDate:, setAnimationTransition:forView:cache:, setAnimationWillStartSelector:, setAnimationsEnabled:, #setContentCompressionResistancePriority:forAxis:, #setContentHuggingPriority:forAxis:, #setNeedsDisplay, #setNeedsDisplayInRect:, #setNeedsLayout, #setNeedsUpdateConstraints, #setTranslatesAutoresizingMaskIntoConstraints:, #sizeThatFits:, #sizeToFit, #systemLayoutSizeFittingSize:, transitionFromView:toView:duration:options:completion:, transitionWithView:duration:options:animations:completion:, #translatesAutoresizingMaskIntoConstraints, #updateConstraints, #updateConstraintsIfNeeded, #viewForBaselineLayout, #viewPrintFormatter, #viewWithTag:, #willMoveToSuperview:, #willMoveToWindow:, #willRemoveSubview:
Methods inherited from UIResponder
#becomeFirstResponder, #canBecomeFirstResponder, #canPerformAction:withSender:, #canResignFirstResponder, #isFirstResponder, #motionBegan:withEvent:, #motionCancelled:withEvent:, #motionEnded:withEvent:, #nextResponder, #reloadInputViews, #remoteControlReceivedWithEvent:, #resignFirstResponder, #touchesBegan:withEvent:, #touchesCancelled:withEvent:, #touchesEnded:withEvent:, #touchesMoved:withEvent:
Methods inherited from NSObject
#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
This class inherits a constructor from NSObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Instance Attribute Details
- (Boolean) allowsMultipleSelection
A Boolean value that determines whether users can select more than one item in the collection view. This property controls whether multiple items can be selected simultaneously. The default value of this property is NO.When the value of this property is YES, tapping a cell adds it to the current selection (assuming the delegate permits the cell to be selected). Tapping the cell again removes it from the selection.
- (Boolean) allowsSelection
A Boolean value that indicates whether users can select items in the collection view. If the value of this property is YES (the default), users can select items. If you want more fine-grained control over the selection of items, you must provide a delegate object and implement the appropriate methods of the UICollectionViewDelegate protocol.
- (UIView) backgroundView
The view that provides the background appearance. The view (if any) in this property is positioned underneath all of the other content and sized automatically to fill the entire bounds of the collection view. The background view does not scroll with the collection view’s other content. The collection view maintains a strong reference to the background view object.This property is nil by default, which displays the background color of the collection view.
- (UICollectionViewLayout) collectionViewLayout
The layout used to organize the collected view’s items. Assigning a new layout object to this property causes the new layout to be applied (without animations) to the collection view’s items.
- (Object) dataSource
The object that provides the data for the collection view. The data source must adopt the UICollectionViewDataSource protocol. The collection view maintains a weak reference to the data source object.
- (Object) delegate
The object that acts as the delegate of the collection view. The delegate must adopt the UICollectionViewDelegate protocol. The collection view maintains a weak reference to the delegate object.The delegate object is responsible for managing selection behavior and interactions with individual items.
Instance Method Details
- (UICollectionViewCell) cellForItemAtIndexPath(indexPath)
Returns the cell object at the specified index path.
- (Object) deleteItemsAtIndexPaths(indexPaths)
Deletes the items at the specified index paths. Use this method to remove items from the collection view. You might do this when you remove the items from your data source object or in response to user interactions with the collection view. The collection view updates the layout of the remaining items to account for the deletions, animating the remaining items into position as needed.You can also call this method from a block passed to the performBatchUpdates:completion: method when you want to animate multiple separate changes into place at the same time. See the description of that method for more information.
- (Object) deleteSections(sections)
Deletes the sections at the specified indexes. Use this method to remove the sections and their items from the collection view. You might do this when you remove the sections from your data source object or in response to user interactions with the collection view. The collection view updates the layout of the remaining sections and items to account for the deletions, animating the remaining items into position as needed.You can also call this method from a block passed to the performBatchUpdates:completion: method when you want to animate multiple separate changes into place at the same time. See the description of that method for more information.
- (Object) dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath:indexPath)
Returns a reusable cell object located by its identifier Call this method from your data source object when asked to provide a new cell for the collection view. This method dequeues an existing cell if one is available or creates a new one based on the class or nib file you previously registered.Important: You must register a class or nib file using the registerClass:forCellWithReuseIdentifier: or registerNib:forCellWithReuseIdentifier: method before calling this method. If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithFrame: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.
- (Object) dequeueReusableSupplementaryViewOfKind(elementKind, withReuseIdentifier:identifier, forIndexPath:indexPath)
Returns a reusable supplementary view located by its identifier and kind. Call this method from your data source object when asked to provide a new supplementary view for the collection view. This method dequeues an existing view if one is available or creates a new one based on the class or nib file you previously registered.Important: You must register a class or nib file using the registerClass:forSupplementaryViewOfKind:withReuseIdentifier: or registerNib:forSupplementaryViewOfKind:withReuseIdentifier: method before calling this method. You can also register a set of default supplementary views with the layout object using the registerClass:forDecorationViewOfKind: or registerNib:forDecorationViewOfKind: method. If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithFrame: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.
- (Object) deselectItemAtIndexPath(indexPath, animated:animated)
Deselects the item at the specified index. If the allowsSelection property is NO, calling this method has no effect. This method does not cause any selection-related delegate methods to be called.
- (NSIndexPath) indexPathForCell(cell)
Returns the index path of the specified cell.
- (NSIndexPath) indexPathForItemAtPoint(point)
Returns the index path of the item at the specified point in the collection view. This method relies on the layout information provided by the associated layout object to determine which item contains the point.
- (Array) indexPathsForSelectedItems
Returns the index paths for the selected items.
- (Array) indexPathsForVisibleItems
Returns an array of the visible items in the collection view.
- (Object) initWithFrame(frame, collectionViewLayout:layout)
Initializes and returns a newly allocated collection view object with the specified frame and layout. Use this method when initializing a collection view object programmatically. If you specify nil for the layout parameter, you must assign a layout object to the collectionViewLayout property before displaying the collection view onscreen. If you do not, the collection view will be unable to present any items onscreen. This method is the designated initializer.
- (Object) insertItemsAtIndexPaths(indexPaths)
Inserts new items at the specified index paths. Call this method to insert one or more new items into the collection view. You might do this when your data source object receives data for new items or in response to user interactions with the collection view. The collection view gets the layout information for the new cells as part of calling this method. And if the layout information indicates that the cells should appear onscreen, the collection view asks your data source to provide the appropriate views, animating them into position as needed.You can also call this method from a block passed to the performBatchUpdates:completion: method when you want to animate multiple separate changes into place at the same time. See the description of that method for more information.
- (Object) insertSections(sections)
Inserts new sections at the specified indexes. Use this method to insert one or more sections into the collection view. This method adds the sections, and it is up to your data source to report the number of items in each section when asked for the information. The collection view then uses that information to get updated layout attributes for the newly inserted sections and items. If the insertions cause a change in the collection view’s visible content, those changes are animated into place. You can also call this method from a block passed to the performBatchUpdates:completion: method when you want to animate multiple separate changes into place at the same time. See the description of that method for more information.
- (UICollectionViewLayoutAttributes) layoutAttributesForItemAtIndexPath(indexPath)
Returns the layout information for the item at the specified index path. Use this method to retrieve the layout information for a particular item. You should always use this method instead of querying the layout object directly.
- (UICollectionViewLayoutAttributes) layoutAttributesForSupplementaryElementOfKind(kind, atIndexPath:indexPath)
Returns the layout information for the specified supplementary view. Use this method to retrieve the layout information for a particular supplementary view. You should always use this method instead of querying the layout object directly.
- (Object) moveItemAtIndexPath(indexPath, toIndexPath:newIndexPath)
Moves an item from one location to another in the collection view. Use this method to reorganize existing data items. You might do this when you rearrange the items within your data source object or in response to user interactions with the collection view. You can move items between sections or within the same section. The collection view updates the layout as needed to account for the move, animating cells into position as needed.You can also call this method from a block passed to the performBatchUpdates:completion: method when you want to animate multiple separate changes into place at the same time. See the description of that method for more information.
- (Object) moveSection(section, toSection:newSection)
Moves a section from one location to another in the collection view. Use this method to reorganize existing sections and their contained items. You might do this when you rearrange sections within your data source object or in response to user interactions with the collection view. The collection view updates the layout as needed to account for the move, animating new views into position as needed.You can also call this method from a block passed to the performBatchUpdates:completion: method when you want to animate multiple separate changes into place at the same time. See the description of that method for more information.
- (Integer) numberOfItemsInSection(section)
Returns the number of items in the specified section.
- (Integer) numberOfSections
Returns the number of sections displayed by the collection view.
- (Object) performBatchUpdates(updates, completion:completion)
Animates multiple insert, delete, reload, and move operations as a group. You can use this method in cases where you want to insert, delete, reload or move cells around the collection view in one single animated operation, as opposed to in several separate animations. Use the blocked passed in the updates parameter to specify all of the operations you want to perform. When you group operations to insert, delete, reload, or move sections inside a single batch job, all operations are performed based on the current indexes of the collection view. This is unlike modifying a mutable array where the insertion or deletion of items affects the indexes of successive operations. Therefore, you do not have to remember which items or sections were inserted, deleted, or moved and adjust the indexes of all other operations accordingly.
- (Object) registerClass(cellClass, forCellWithReuseIdentifier:identifier)
Register a class for use in creating new collection view cells. Prior to calling the dequeueReusableCellWithReuseIdentifier:forIndexPath: method of the collection view, you must use this method or the registerNib:forCellWithReuseIdentifier: method to tell the collection view how to create a new cell of the given type. If a cell of the specified type is not currently in a reuse queue, the collection view uses the provided information to create a new cell object automatically. If you previously registered a class or nib file with the same reuse identifier, the class you specify in the cellClass parameter replaces the old entry. You may specify nil for cellClass if you want to unregister the class from the specified reuse identifier.
- (Object) registerClass(viewClass, forSupplementaryViewOfKind:elementKind, withReuseIdentifier:identifier)
Registers a class for use in creating supplementary views for the collection view. Prior to calling the dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: method of the collection view, you must use this method or the registerNib:forSupplementaryViewOfKind:withReuseIdentifier: method to tell the collection view how to create a supplementary view of the given type. If a view of the specified type is not currently in a reuse queue, the collection view uses the provided information to create a view object automatically. If you previously registered a class or nib file with the same element kind and reuse identifier, the class you specify in the viewClass parameter replaces the old entry. You may specify nil for viewClass if you want to unregister the class from the specified element kind and reuse identifier.
- (Object) registerNib(nib, forCellWithReuseIdentifier:identifier)
Register a nib file for use in creating new collection view cells. Prior to calling the dequeueReusableCellWithReuseIdentifier:forIndexPath: method of the collection view, you must use this method or the registerClass:forCellWithReuseIdentifier: method to tell the collection view how to create a new cell of the given type. If a cell of the specified type is not currently in a reuse queue, the collection view uses the provided information to create a new cell object automatically. If you previously registered a class or nib file with the same reuse identifier, the object you specify in the nib parameter replaces the old entry. You may specify nil for nib if you want to unregister the nib file from the specified reuse identifier.
- (Object) registerNib(nib, forSupplementaryViewOfKind:kind, withReuseIdentifier:identifier)
Registers a nib file for use in creating supplementary views for the collection view. Prior to calling the dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: method of the collection view, you must use this method or the registerClass:forSupplementaryViewOfKind:withReuseIdentifier: method to tell the collection view how to create a supplementary view of the given type. If a view of the specified type is not currently in a reuse queue, the collection view uses the provided information to create a view object automatically. If you previously registered a class or nib file with the same element kind and reuse identifier, the class you specify in the viewClass parameter replaces the old entry. You may specify nil for nib if you want to unregister the class from the specified element kind and reuse identifier.
- (Object) reloadData
Reloads all of the data for the collection view. Call this method to reload all of the items in the collection view. This causes the collection view to discard any currently visible items and redisplay them. For efficiency, the collection view only displays those cells and supplementary views that are visible. If the collection data shrinks as a result of the reload, the collection view adjusts its scrolling offsets accordingly. You should not call this method in the middle of animation blocks where items are being inserted or deleted. Insertions and deletions automatically cause the table’s data to be updated appropriately.
- (Object) reloadItemsAtIndexPaths(indexPaths)
Reloads just the items at the specified index paths. Call this method to selectively reload only the specified items. This causes the collection view to discard any cells associated with those items and redisplay them.
- (Object) reloadSections(sections)
Reloads the data in the specified sections of the collection view. Call this method to selectively reload only the items in the specified sections. This causes the collection view to discard any cells associated with those items and redisplay them.
- (Object) scrollToItemAtIndexPath(indexPath, atScrollPosition:scrollPosition, animated:animated)
Scrolls the collection view contents until the specified item is visible.
- (Object) selectItemAtIndexPath(indexPath, animated:animated, scrollPosition:scrollPosition)
Selects the item at the specified index path and optionally scrolls it into view. If the allowsSelection property is NO, calling this method has no effect. If there is an existing selection with a different index path and the allowsMultipleSelection property is NO, calling this method replaces the previous selection.This method does not cause any selection-related delegate methods to be called.
- (Object) setCollectionViewLayout(layout, animated:animated)
Assigns a new layout object to the collection view and optionally animates the change. When animating layout changes, the animation timing and parameters are controlled by the collection view.
- (Array) visibleCells
Returns an array of visible cells currently displayed by the collection view. This method returns the complete list of visible cells displayed by the collection view.