Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added selection

  • Loading branch information...
commit 7a671332cde6514aef9e99fade8e4571744287ef 1 parent 137e756
@soffes soffes authored
View
4 SSToolkit/SSCollectionView.h
@@ -33,6 +33,7 @@
CGFloat _minimumColumnSpacing;
CGFloat _rowSpacing;
BOOL _allowsSelection;
+ NSMutableSet *_visibleItems;
NSMutableDictionary *_reuseableItems;
UITableView *_tableView;
@@ -167,6 +168,9 @@
- (CGFloat)collectionView:(SSCollectionView *)aCollectionView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)collectionView:(SSCollectionView *)aCollectionView heightForFooterInSection:(NSInteger)section;
+- (void)collectionView:(SSCollectionView *)aCollectionView willSelectItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(SSCollectionView *)aCollectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
+- (void)collectionView:(SSCollectionView *)aCollectionView willDeselectItemAtIndexPath:(NSIndexPath *)indexPath;
+- (void)collectionView:(SSCollectionView *)aCollectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;
@end
View
47 SSToolkit/SSCollectionView.m
@@ -34,6 +34,10 @@ - (void)dealloc {
self.dataSource = nil;
self.delegate = nil;
+ [_visibleItems removeAllObjects];
+ [_visibleItems release];
+ _visibleItems = nil;
+
[_reuseableItems removeAllObjects];
[_reuseableItems release];
_reuseableItems = nil;
@@ -57,6 +61,7 @@ - (id)initWithFrame:(CGRect)frame {
_rowSpacing = 20.0f;
_allowsSelection = YES;
_reuseableItems = [[NSMutableDictionary alloc] init];
+ _visibleItems = [[NSMutableSet alloc] init];
_tableView = [[UITableView alloc] initWithFrame:CGRectSetZeroOrigin(frame)];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
@@ -95,7 +100,12 @@ - (SSCollectionViewItem *)dequeueReusableItemWithIdentifier:(NSString *)identifi
- (SSCollectionViewItem *)itemPathForIndex:(NSIndexPath *)indexPath {
- // TODO: Implement
+ for (SSCollectionViewItem *item in _visibleItems) {
+ if ([item.indexPath isEqual:indexPath]) {
+ return item;
+ }
+ }
+
return nil;
}
@@ -107,11 +117,16 @@ - (NSIndexPath *)indexPathForItem:(SSCollectionViewItem *)item {
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated {
+ // Notify delegate that it will select
+ if ([self.delegate respondsToSelector:@selector(collectionView:willSelectItemAtIndexPath:)]) {
+ [self.delegate collectionView:self willSelectItemAtIndexPath:indexPath];
+ }
+
SSCollectionViewItem *item = [self itemPathForIndex:indexPath];
[item setHighlighted:NO animated:NO];
[item setSelected:YES animated:YES];
- // Notify delegate of selection
+ // Notify delegate that it did selection
if ([self.delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)]) {
[self.delegate collectionView:self didSelectItemAtIndexPath:indexPath];
}
@@ -119,7 +134,19 @@ - (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated {
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated {
- // TODO: Implement
+ // Notify delegate that it will deselect
+ if ([self.delegate respondsToSelector:@selector(collectionView:willDeselectItemAtIndexPath:)]) {
+ [self.delegate collectionView:self willDeselectItemAtIndexPath:indexPath];
+ }
+
+ SSCollectionViewItem *item = [self itemPathForIndex:indexPath];
+ [item setHighlighted:NO animated:NO];
+ [item setSelected:NO animated:YES];
+
+ // Notify delegate that it did deselection
+ if ([self.delegate respondsToSelector:@selector(collectionView:didDeselectItemAtIndexPath:)]) {
+ [self.delegate collectionView:self didDeselectItemAtIndexPath:indexPath];
+ }
}
@@ -136,6 +163,8 @@ - (void)reloadItemAtIndexPaths:(NSIndexPath *)indexPaths {
#pragma mark Private Methods
- (void)_reuseItem:(SSCollectionViewItem *)item {
+ [_visibleItems removeObject:item];
+
NSMutableArray *items = [_reuseableItems objectForKey:item.reuseIdentifier];
if (!items) {
[_reuseableItems setObject:[NSMutableArray array] forKey:item.reuseIdentifier];
@@ -152,12 +181,6 @@ - (void)_reuseItems:(NSArray *)items {
}
-- (void)_selectItem:(SSCollectionViewItem *)item animated:(BOOL)animated {
- // TODO: Since the item is being displayed, we could probably lookup the index path quick than this way
- [self selectItemAtIndexPath:[self indexPathForItem:item] animated:animated];
-}
-
-
- (CGSize)_itemSizeForSection:(NSInteger)section {
// TODO: Cache this value to elminate lots of method calls
if ([_delegate respondsToSelector:@selector(collectionView:itemSizeForSection:)] == NO) {
@@ -187,8 +210,8 @@ - (NSArray *)_itemsForRowIndexPath:(NSIndexPath *)rowIndexPath {
NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:endIndex - startIndex];
for (NSInteger i = startIndex; i < endIndex; i++) {
- // TODO: Store item so it can be dequeued later
- SSCollectionViewItem *item = [_dataSource collectionView:self itemForIndexPath:[NSIndexPath indexPathForRow:i inSection:rowIndexPath.section]];
+ NSIndexPath *itemIndexPath = [NSIndexPath indexPathForRow:i inSection:rowIndexPath.section];
+ SSCollectionViewItem *item = [_dataSource collectionView:self itemForIndexPath:itemIndexPath];
if (item == nil) {
NSException *exception = [NSException exceptionWithName:kSSCollectionViewNilItemExceptionName
reason:kSSCollectionViewNilItemExceptionReason userInfo:nil];
@@ -197,7 +220,9 @@ - (NSArray *)_itemsForRowIndexPath:(NSIndexPath *)rowIndexPath {
}
item.tag = i;
+ item.indexPath = itemIndexPath;
item.collectionView = self;
+ [_visibleItems addObject:item];
[items addObject:item];
}
View
1  SSToolkit/SSCollectionViewInternal.h
@@ -10,6 +10,5 @@
- (void)_reuseItems:(NSArray *)items;
- (void)_reuseItem:(SSCollectionViewItem *)item;
-- (void)_selectItem:(SSCollectionViewItem *)item animated:(BOOL)animated;
@end
View
7 SSToolkit/SSCollectionViewItem.m
@@ -23,11 +23,14 @@ @implementation SSCollectionViewItem
@synthesize reuseIdentifier = _reuseIdentifier;
@synthesize selected = _selected;
@synthesize highlighted = _highlighted;
+@synthesize indexPath = _indexPath;
@synthesize collectionView = _collectionView;
#pragma mark NSObject
- (void)dealloc {
+ self.collectionView = nil;
+ [_indexPath release];
[_imageView release];
[_textLabel release];
[_detailTextLabel release];
@@ -53,8 +56,8 @@ - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self setHighlighted:YES animated:NO];
- if (CGRectContainsPoint(self.frame, [[touches anyObject] locationInView:self])) {
- [self.collectionView _selectItem:self animated:YES];
+ if (CGRectContainsPoint(CGRectSetZeroOrigin(self.frame), [[touches anyObject] locationInView:self])) {
+ [self.collectionView selectItemAtIndexPath:self.indexPath animated:YES];
}
}
View
1  SSToolkit/SSCollectionViewItemInternal.h
@@ -10,6 +10,7 @@
@interface SSCollectionViewItem ()
+@property (nonatomic, retain) NSIndexPath *indexPath;
@property (nonatomic, assign) SSCollectionView *collectionView;
@end
Please sign in to comment.
Something went wrong with that request. Please try again.