UICollectionView provides a more flexible way to display data compared to UITableView. It handles grids, horizontal scrolling, and custom layouts. The API is similar to UITableView with data sources, delegates, and cell/view reuse. UICollectionViewFlowLayout provides an easy way to create basic grids that can be customized. Fully custom layouts require subclassing UICollectionViewLayout to position cells and views.
1 of 78
Download to read offline
More Related Content
Introducing collection views - Mark Pospesel
1. Introducing Collection Views
Mark Pospesel
Mobile Architect
Y Media Labs
code github.com/mpospese/IntroducingCollectionViews
slides bit.ly/ViTNiK
@mpospese
際際滷s and code on
?ash drives
2. Who Am I?
? Studied mathematics, marine biology, and teaching English as a Second
Language
? 14 years industry experience
? Programming for mobile since 1999!
? Mobile Architect for Y Media Labs
7. Grid views and horizontal scrollers
The hard way
? UIScrollView
? manually place ^cells ̄
? DataSource delegate
? delegate for selection, appearance, disappearance, etc.
? create views for cells on demand and cache cells for reuse
8. What if you wanted something even more complex?
9. Enter UICollectionView
? Handles grids
? Handles horizontal line scrollers
? Handles pretty much any custom layout you can imagine
? High-performance even with large data sets
? Works well with Core Data and NSFetchedResultsController
? Familiar delegate / data source API pattern
18. UICollectionView
? If you know how to use UITableView, you mostly know how to use simple
Collection Views already
19. UICollectionViewDataSource
? number of sections
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
? number of items in each section
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:
(NSInteger)section
? con?gure cells
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
? con?gure supplementary views
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
20. Cell and view reuse
? Cells, supplementary views, and decoration views are all reused
? Each type of view needs to be registered for reuse (nib or class)
registerClass:forCellWithReuseIdentifier:
registerNib:forCellWithReuseIdentifier:
registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
21. Cell and view reuse
? Cells, supplementary views, and decoration views are all reused
? Each type of view needs to be registered for reuse (nib or class)
? dequeue will instantiate the class if necessary
dequeueReusableCellWithReuseIdentifier:forIndexPath:
dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
22. UICollectionViewDelegate
? Manages selecting and highlighting
? should/did select/deselect
? should/did highlight/unhighlight
? Tracks the removal of cells
? didEndDisplay cell/supplementary view
? Manages actions for cells
? should show menu
? canPerform/perform action on cell
25. UICollectionViewFlowLayout
? Subclass of UICollectionViewLayout for grids
? Makes it super easy to implement basic grids
? Supports headers and footers
? Can be customized via properties or by delegate
? property = global
? delegate = per item or section
? Can be subclassed
49. UICollectionViewLayoutAttributes
? Describe position, size, visual state of collection items
? Apply to cells, supplementary views, and decoration views
? Set by layout
? Used by collection view to con?gure cells and views
? Subclass to add additional attributes
51. When to subclass UICollectionViewFlowLayout
? add decoration views
? add extra supplementary views (beyond headers and footers)
? adjust the default layout attributes of items
? add new layout attributes
? use custom insert or delete animations
52. When to subclass UICollectionViewLayout
? The layout you want is nothing like a grid
? You¨d have to change the default attributes so much that it would be less
work to not derive from ?ow layout
53. How UICollectionViewLayout works
? prepareLayout
? collectionViewContentSize
? layoutAttributesForElementsInRect:
? provide layout attributes on demand
layoutAttributesForItemAtIndexPath:
layoutAttributesForSupplementaryViewOfKind:atIndexPath:
layoutAttributesForDecorationViewOfKind:atIndexPath:
? invalidateLayout
54. Invalidating layouts
? explicit: invalidateLayout
? implicit: performBatchUpdates:completion:
? implicit: override shouldInvalidateLayoutForBoundsChange:
? called when contentO?set changes or when bounds change
? always return YES
? return YES only if bounds change
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)oldBounds
55. Miscellaneous Tips
? don¨t use collectionView.frame / bounds outside of prepareLayout
? consider setting hidden = YES instead of alpha = 0
59. How to add decoration views
? Create your own layout class
? Register your decoration view
registerNib:forDecorationViewOfKind:
registerClass:forDecorationViewOfKind:
? Return attributes for all decoration views
layoutAttributesForElementsInRect:
layoutAttributesForDecorationViewOfKind:atIndexPath:
60. When to subclass UICollectionViewLayoutAttributes
? You need additional attributes beyond those found in the base class
61. How to create custom attributes
? Subclass UICollectionViewLayoutAttributes
? add additional properties
? Important! You must implement copyWithZone:
- (id)copyWithZone:(NSZone *)zone
{
CustomAttributes *attributes = [super copyWithZone:zone];
attributes.customProperty = self.customProperty;
return attributes;
}
62. How to create custom attributes
? Subclass UICollectionViewLayoutAttributes
? Subclass UICollectionViewLayout (or ?ow layout)
? implement layoutAttributesClass to return your custom class
+ (Class)layoutAttributesClass
{
return [CustomAttributes class];
}
63. How to create custom attributes
? Subclass UICollectionViewLayoutAttributes
? Subclass UICollectionViewLayout (or ?ow layout)
? implement layoutAttributesClass to return your custom class
? set your custom attributes
layoutAttributesForElementsInRect:
layoutAttributesForItemAtIndexPath:
layoutAttributesForSupplementaryViewOfKind:atIndexPath:
layoutAttributesForDecorationViewOfKind:atIndexPath:
64. How to create custom attributes
? Subclass UICollectionViewLayoutAttributes
? Subclass UICollectionViewLayout (or ?ow layout)
? In your cell, supplementary view, or decoration view classes implement
applyLayoutAttributes:
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
if ([layoutAttributes isKindOfClass:[CustomAttributes class]])
{
CustomAttributes *customAttributes = (CustomAttributes *)layoutAttributes;
// Do something interesting with them
}
}
69. ? override ?nalLayoutAttributesForDisappearingItemAtIndexPath:
? gets called for every item that needs to be moved too, so need to track
index of deleted item(s).
? do that in prepareForCollectionViewUpdates: (don¨t forget to call super!)
Customizing delete animations
What they didn¨t tell you
- (void)prepareForCollectionViewUpdates:(NSArray *)updateItems
{
[super prepareForCollectionViewUpdates:updateItems];
self.deleteIndexPaths = [NSMutableArray array];
for (UICollectionViewUpdateItem *update in updateItems)
{
if (update.updateAction == UICollectionUpdateActionDelete)
{
[self.deleteIndexPaths addObject:update.indexPathBeforeUpdate];
}
}
}
70. ? override ?nalLayoutAttributesForDisappearingItemAtIndexPath:
? gets called for every item that needs to be moved too, so need to track
index of deleted item(s).
? do that in prepareForCollectionViewUpdates: (don¨t forget to call super!)
? clean up in ?nalizeCollectionViewUpdates
Customizing delete animations
What they didn¨t tell you
- (void)finalizeCollectionViewUpdates
{
[super finalizeCollectionViewUpdates];
self.deleteIndexPaths = nil;
}
71. ? override ?nalLayoutAttributesForDisappearingItemAtIndexPath:
Customizing delete animations
What they didn¨t tell you
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:
(NSIndexPath *)itemIndexPath
{
if ([self.deleteIndexPaths containsObject:itemIndexPath])
{
UICollectionViewLayoutAttributes* attributes = [self
layoutAttributesForItemAtIndexPath:itemIndexPath];
// Configure attributes ...
return attributes;
}
return nil;
}
72. ? same applies to initialLayoutAttributesForAppearingItemAtIndexPath:
? will get called for both insert and delete animations
? must call super
Customizing delete animations
But wait, there¨s more!
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:
(NSIndexPath *)itemIndexPath
{
UICollectionViewLayoutAttributes *attributes = [super
initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];
if ([self.insertIndexPaths containsObject:itemIndexPath])
{
if (!attributes)
attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
// Configure attributes ...
}
return attributes;
}
75. Summary
? Collection views are very powerful, ?exible tool
? UICollectionViewFlowLayout is highly customizable via properties,
subclassing, or delegation
? UICollectionView can support any arbitrary layout
? If you can dream it, build a layout for it!
76. Support for iOS 4.3 & 5
PSTCollectionView
? GitHub project by Peter Steinberger
? uses UICollectionView classes under iOS 6
? uses custom classes under iOS 4.3 and 5
? doesn¨t have all the animations (yet)
? github.com/steipete/PSTCollectionView