diff --git a/TMQuiltView/TMQuiltView/TMQuiltView.h b/TMQuiltView/TMQuiltView/TMQuiltView.h index a6a5537..e03c41e 100644 --- a/TMQuiltView/TMQuiltView/TMQuiltView.h +++ b/TMQuiltView/TMQuiltView/TMQuiltView.h @@ -60,6 +60,8 @@ typedef enum { @property (nonatomic, assign) id dataSource; @property (nonatomic, assign) id delegate; +- (NSIndexPath*) indexPathForCell: (TMQuiltViewCell*) cell; + // Returns the cell if it's visible and indexPath is valid. Returns nil otherwise - (TMQuiltViewCell *)cellAtIndexPath:(NSIndexPath*)indexPath; diff --git a/TMQuiltView/TMQuiltView/TMQuiltView.m b/TMQuiltView/TMQuiltView/TMQuiltView.m index abad306..4120299 100644 --- a/TMQuiltView/TMQuiltView/TMQuiltView.m +++ b/TMQuiltView/TMQuiltView/TMQuiltView.m @@ -124,12 +124,22 @@ - (id)initWithFrame:(CGRect)frame self = [super initWithFrame:frame]; if (self) { super.alwaysBounceVertical = YES; + self.backgroundColor = [UIColor blackColor]; [self addGestureRecognizer:self.tapGestureRecognizer]; + _numberOfColumms = kTMQuiltViewDefaultColumns; } return self; } +- (void)awakeFromNib { + super.alwaysBounceVertical = YES; + self.backgroundColor = [UIColor blackColor]; + [self addGestureRecognizer:self.tapGestureRecognizer]; + + _numberOfColumms = kTMQuiltViewDefaultColumns; +} + - (void)setDelegate:(id)delegate { [super setDelegate:delegate]; } @@ -225,6 +235,21 @@ - (NSInteger)numberOfColumns { return numberOfColumns; } +- (NSIndexPath*) indexPathForCell: (TMQuiltViewCell*) cell { + __block NSIndexPath* path = nil; + for(int i = 0; i < _numberOfColumms; i++) { + NSMutableDictionary* indexPathsToViews = self.indexPathToViewByColumn[i]; + [indexPathsToViews enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + if (obj == cell) { + *stop = YES; + path = key; + } + }]; + if (path) return path; + } + return nil; +} + /** Excerpt from UITableView doc: @@ -566,6 +591,8 @@ - (void)viewTapped:(UIGestureRecognizer *)recognizer { while((indexPath = (NSIndexPath *)[displayedViewEnumerator nextObject])) { TMQuiltViewCell *photoCell = [self.indexPathToViewByColumn[i] objectForKey:indexPath]; if (CGRectContainsPoint(photoCell.frame, tapPoint)) { + if (!photoCell.userInteractionEnabled) break; + photoCell.selected = YES; if ([self.delegate respondsToSelector:@selector(quiltView:didSelectCellAtIndexPath:)]) { [self.delegate quiltView:self didSelectCellAtIndexPath:indexPath]; diff --git a/TMQuiltViewDemo/TMQuiltViewDemo/TMPhotoQuiltViewCell.h b/TMQuiltViewDemo/TMQuiltViewDemo/TMPhotoQuiltViewCell.h index e9f4209..7a6055b 100644 --- a/TMQuiltViewDemo/TMQuiltViewDemo/TMPhotoQuiltViewCell.h +++ b/TMQuiltViewDemo/TMQuiltViewDemo/TMPhotoQuiltViewCell.h @@ -18,10 +18,12 @@ // #import +#import "TMQuiltView.h" @interface TMPhotoQuiltViewCell : TMQuiltViewCell @property (nonatomic, retain) UIImageView *photoView; @property (nonatomic, retain) UILabel *titleLabel; +@property (nonatomic, retain) UIImageView *starView; @end diff --git a/TMQuiltViewDemo/TMQuiltViewDemo/TMPhotoQuiltViewCell.m b/TMQuiltViewDemo/TMQuiltViewDemo/TMPhotoQuiltViewCell.m index d3d7e21..f949772 100644 --- a/TMQuiltViewDemo/TMQuiltViewDemo/TMPhotoQuiltViewCell.m +++ b/TMQuiltViewDemo/TMQuiltViewDemo/TMPhotoQuiltViewCell.m @@ -26,6 +26,7 @@ @implementation TMPhotoQuiltViewCell @synthesize photoView = _photoView; @synthesize titleLabel = _titleLabel; +@synthesize starView = _starView; - (void)dealloc { [_photoView release], _photoView = nil; @@ -43,6 +44,17 @@ - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier return self; } +- (UIImageView *)starView { + if (!_starView) { + _starView = [[UIImageView alloc] init]; + _starView.contentMode = UIViewContentModeTopRight; + _starView.clipsToBounds = YES; + _starView.image = [UIImage imageNamed: @"star"]; + [self addSubview:_starView]; + } + return _starView; +} + - (UIImageView *)photoView { if (!_photoView) { _photoView = [[UIImageView alloc] init]; @@ -58,6 +70,7 @@ - (UILabel *)titleLabel { _titleLabel = [[UILabel alloc] init]; _titleLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5]; _titleLabel.textColor = [UIColor whiteColor]; + _titleLabel.font = [UIFont boldSystemFontOfSize: 14]; _titleLabel.textAlignment = UITextAlignmentCenter; [self addSubview:_titleLabel]; } @@ -68,6 +81,7 @@ - (void)layoutSubviews { self.photoView.frame = CGRectInset(self.bounds, kTMPhotoQuiltViewMargin, kTMPhotoQuiltViewMargin); self.titleLabel.frame = CGRectMake(kTMPhotoQuiltViewMargin, self.bounds.size.height - 20 - kTMPhotoQuiltViewMargin, self.bounds.size.width - 2 * kTMPhotoQuiltViewMargin, 20); + self.starView.frame = CGRectInset(self.bounds, 5, 5); } @end