Permalink
Browse files

Organized collection view implementation

  • Loading branch information...
1 parent fc1acd8 commit bf40ee1724584733da8800134f6aa5c60e44bc6f @soffes soffes committed Oct 18, 2011
Showing with 165 additions and 153 deletions.
  1. +165 −153 SSToolkit/SSCollectionView.m
@@ -69,75 +69,6 @@ @implementation SSCollectionView {
@synthesize allowsSelection = _allowsSelection;
@synthesize extremitiesStyle = _extremitiesStyle;
-- (void)setDataSource:(id<SSCollectionViewDataSource>)dataSource {
- _dataSource = dataSource;
-
- if (_delegate && _dataSource) {
- [self reloadData];
- }
-}
-
-
-- (void)setDelegate:(id<SSCollectionViewDelegate>)delegate {
- _delegate = delegate;
-
- if (_delegate && _dataSource) {
- [self reloadData];
- }
-}
-
-
-- (UIScrollView *)scrollView {
- return _tableView;
-}
-
-
-- (UIView *)backgroundView {
- return _tableView.backgroundView;
-}
-
-
-- (void)setBackgroundView:(UIView *)background {
- _tableView.backgroundView = background;
-}
-
-
-- (UIView *)collectionHeaderView {
- return _tableView.tableHeaderView;
-}
-
-
-- (void)setCollectionHeaderView:(UIView *)collectionHeaderView {
- _tableView.tableHeaderView = collectionHeaderView;
-}
-
-
-- (UIView *)collectionFooterView {
- return _tableView.tableFooterView;
-}
-
-
-- (void)setCollectionFooterView:(UIView *)collectionFooterView {
- _tableView.tableFooterView = collectionFooterView;
-}
-
-
-- (void)setBackgroundColor:(UIColor *)color {
- [super setBackgroundColor:color];
- _tableView.backgroundColor = color;
-}
-
-
-- (void)setExtremitiesStyle:(SSCollectionViewExtremitiesStyle)style {
- if (_extremitiesStyle == style) {
- return;
- }
-
- _extremitiesStyle = style;
- [self reloadData];
-}
-
-
#pragma mark - NSObject
- (void)dealloc {
@@ -196,22 +127,14 @@ - (void)setFrame:(CGRect)frame {
}
-#pragma mark - SSCollectionView
-
-- (void)reloadData {
- if (![self superview]) {
- return;
- }
-
- [_sectionCache removeAllObjects];
- [_tableView reloadData];
-
- if (_extremitiesStyle == SSCollectionViewExtremitiesStyleScrolling) {
-
- }
+- (void)setBackgroundColor:(UIColor *)color {
+ [super setBackgroundColor:color];
+ _tableView.backgroundColor = color;
}
+#pragma mark - Configuring a Collection View
+
- (SSCollectionViewItem *)dequeueReusableItemWithIdentifier:(NSString *)identifier {
if (!identifier) {
return nil;
@@ -230,6 +153,70 @@ - (SSCollectionViewItem *)dequeueReusableItemWithIdentifier:(NSString *)identifi
}
+- (NSUInteger)numberOfItemsInSection:(NSUInteger)section {
+ NSNumber *items = [self _sectionInfoItemForKey:kSSCollectionViewSectionNumberOfItemsKey section:section];
+ if (!items) {
+ if ([_dataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)] == NO) {
+ return 0;
+ }
+ items = [NSNumber numberWithUnsignedInteger:[_dataSource collectionView:self numberOfItemsInSection:section]];
+ [self _setSectionInfoItem:items forKey:kSSCollectionViewSectionNumberOfItemsKey section:section];
+ }
+ return [items unsignedIntegerValue];
+}
+
+
+- (NSUInteger)numberOfSections {
+ if ([_dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]) {
+ return [_dataSource numberOfSectionsInCollectionView:self];
+ }
+
+ return 1;
+}
+
+
+- (UIView *)backgroundView {
+ return _tableView.backgroundView;
+}
+
+
+- (void)setBackgroundView:(UIView *)background {
+ _tableView.backgroundView = background;
+}
+
+
+- (UIView *)collectionHeaderView {
+ return _tableView.tableHeaderView;
+}
+
+
+- (void)setCollectionHeaderView:(UIView *)collectionHeaderView {
+ _tableView.tableHeaderView = collectionHeaderView;
+}
+
+
+- (UIView *)collectionFooterView {
+ return _tableView.tableFooterView;
+}
+
+
+- (void)setCollectionFooterView:(UIView *)collectionFooterView {
+ _tableView.tableFooterView = collectionFooterView;
+}
+
+
+- (void)setExtremitiesStyle:(SSCollectionViewExtremitiesStyle)style {
+ if (_extremitiesStyle == style) {
+ return;
+ }
+
+ _extremitiesStyle = style;
+ [self reloadData];
+}
+
+
+#pragma mark - Accessing Items and Sections
+
- (SSCollectionViewItem *)itemForIndexPath:(NSIndexPath *)indexPath {
__block SSCollectionViewItem *item = nil;
[_visibleItems enumerateObjectsUsingBlock:^(id object, BOOL *stop) {
@@ -247,6 +234,35 @@ - (NSIndexPath *)indexPathForItem:(SSCollectionViewItem *)item {
}
+- (NSArray *)visibleItems {
+ return [_visibleItems allObjects];
+}
+
+
+- (NSArray *)indexPathsForVisibleRows {
+ NSMutableArray *indexPaths = [[NSMutableArray alloc] initWithCapacity:[_visibleItems count]];
+ for (SSCollectionViewItem *item in _visibleItems) {
+ [indexPaths addObject:[self indexPathForItem:item]];
+ }
+ return [indexPaths autorelease];
+}
+
+
+#pragma mark - Scrolling the Collection View
+
+- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(SSCollectionViewScrollPosition)scrollPosition animated:(BOOL)animated {
+ NSIndexPath *cellIndexPath = [self _cellIndexPathFromItemIndexPath:indexPath];
+ [_tableView scrollToRowAtIndexPath:cellIndexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:animated];
+}
+
+
+- (UIScrollView *)scrollView {
+ return _tableView;
+}
+
+
+#pragma mark - Managing Selections
+
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(SSCollectionViewScrollPosition)scrollPosition {
// Notify delegate that it will select
if ([self.delegate respondsToSelector:@selector(collectionView:willSelectItemAtIndexPath:)]) {
@@ -289,77 +305,6 @@ - (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
}
-- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(SSCollectionViewScrollPosition)scrollPosition animated:(BOOL)animated {
- NSIndexPath *cellIndexPath = [self _cellIndexPathFromItemIndexPath:indexPath];
- [_tableView scrollToRowAtIndexPath:cellIndexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:animated];
-}
-
-
-- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths {
- NSMutableArray *rowIndexPaths = [[NSMutableArray alloc] init];
- [indexPaths enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
- NSIndexPath *itemIndexPath = (NSIndexPath *)object;
- NSIndexPath *rowIndexPath = [self _cellIndexPathFromItemIndexPath:itemIndexPath];
- if (![rowIndexPaths containsObject:rowIndexPath]) {
- [rowIndexPaths addObject:rowIndexPath];
- }
- }];
- [_tableView reloadRowsAtIndexPaths:rowIndexPaths withRowAnimation:UITableViewRowAnimationFade];
- [rowIndexPaths release];
-}
-
-
-- (NSUInteger)numberOfSections {
- if ([_dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]) {
- return [_dataSource numberOfSectionsInCollectionView:self];
- }
-
- return 1;
-}
-
-
-- (NSUInteger)numberOfItemsInSection:(NSUInteger)section {
- NSNumber *items = [self _sectionInfoItemForKey:kSSCollectionViewSectionNumberOfItemsKey section:section];
- if (!items) {
- if ([_dataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)] == NO) {
- return 0;
- }
- items = [NSNumber numberWithUnsignedInteger:[_dataSource collectionView:self numberOfItemsInSection:section]];
- [self _setSectionInfoItem:items forKey:kSSCollectionViewSectionNumberOfItemsKey section:section];
- }
- return [items unsignedIntegerValue];
-}
-
-
-- (CGRect)rectForSection:(NSUInteger)section {
- return [_tableView rectForSection:(NSInteger)section];
-}
-
-
-- (CGRect)rectForHeaderInSection:(NSUInteger)section {
- return [_tableView rectForSection:(NSInteger)section];
-}
-
-
-- (CGRect)rectForFooterInSection:(NSUInteger)section {
- return [_tableView rectForFooterInSection:(NSInteger)section];
-}
-
-
-- (NSArray *)visibleItems {
- return [_visibleItems allObjects];
-}
-
-
-- (NSArray *)indexPathsForVisibleRows {
- NSMutableArray *indexPaths = [[NSMutableArray alloc] initWithCapacity:[_visibleItems count]];
- for (SSCollectionViewItem *item in _visibleItems) {
- [indexPaths addObject:[self indexPathForItem:item]];
- }
- return [indexPaths autorelease];
-}
-
-
#pragma mark - Inserting, Deleting, and Moving Items and Sections
- (void)beginUpdates {
@@ -466,6 +411,73 @@ - (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection {
}
+#pragma mark - Reloading the Collection View
+
+- (void)reloadData {
+ if (![self superview]) {
+ return;
+ }
+
+ [_sectionCache removeAllObjects];
+ [_tableView reloadData];
+
+ if (_extremitiesStyle == SSCollectionViewExtremitiesStyleScrolling) {
+
+ }
+}
+
+
+- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths {
+ NSMutableArray *rowIndexPaths = [[NSMutableArray alloc] init];
+ [indexPaths enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
+ NSIndexPath *itemIndexPath = (NSIndexPath *)object;
+ NSIndexPath *rowIndexPath = [self _cellIndexPathFromItemIndexPath:itemIndexPath];
+ if (![rowIndexPaths containsObject:rowIndexPath]) {
+ [rowIndexPaths addObject:rowIndexPath];
+ }
+ }];
+ [_tableView reloadRowsAtIndexPaths:rowIndexPaths withRowAnimation:UITableViewRowAnimationFade];
+ [rowIndexPaths release];
+}
+
+
+#pragma mark - Accessing Drawing Areas of the Collection View
+
+- (CGRect)rectForSection:(NSUInteger)section {
+ return [_tableView rectForSection:(NSInteger)section];
+}
+
+
+- (CGRect)rectForFooterInSection:(NSUInteger)section {
+ return [_tableView rectForFooterInSection:(NSInteger)section];
+}
+
+
+- (CGRect)rectForHeaderInSection:(NSUInteger)section {
+ return [_tableView rectForSection:(NSInteger)section];
+}
+
+
+#pragma mark - Managing the Delegate and the Data Source
+
+- (void)setDataSource:(id<SSCollectionViewDataSource>)dataSource {
+ _dataSource = dataSource;
+
+ if (_delegate && _dataSource) {
+ [self reloadData];
+ }
+}
+
+
+- (void)setDelegate:(id<SSCollectionViewDelegate>)delegate {
+ _delegate = delegate;
+
+ if (_delegate && _dataSource) {
+ [self reloadData];
+ }
+}
+
+
#pragma mark - Private Methods
- (void)_initialize {

0 comments on commit bf40ee1

Please sign in to comment.