Skip to content

Commit

Permalink
Refactor selection to make it more flexible, update demo accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernesto Rivera committed Aug 20, 2014
1 parent cb0e439 commit 7f0272c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 45 deletions.
3 changes: 1 addition & 2 deletions Classes/EasyTableView.h
Expand Up @@ -48,7 +48,7 @@ typedef NS_ENUM(NSUInteger, EasyTableViewOrientation){
- (UIView *)easyTableView:(EasyTableView *)easyTableView viewForRect:(CGRect)rect;
- (void)easyTableView:(EasyTableView *)easyTableView setDataForView:(UIView *)view forIndexPath:(NSIndexPath*)indexPath;
@optional
- (void)easyTableView:(EasyTableView *)easyTableView selectedView:(UIView *)selectedView atIndexPath:(NSIndexPath *)indexPath deselectedView:(UIView *)deselectedView;
- (void)easyTableView:(EasyTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)easyTableView:(EasyTableView *)easyTableView scrolledToOffset:(CGPoint)contentOffset;
- (void)easyTableView:(EasyTableView *)easyTableView scrolledToFraction:(CGFloat)fraction;
- (NSUInteger)numberOfSectionsInEasyTableView:(EasyTableView*)easyTableView;
Expand All @@ -64,7 +64,6 @@ typedef NS_ENUM(NSUInteger, EasyTableViewOrientation){
@property (nonatomic, weak) id<EasyTableViewDelegate> delegate;
@property (nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, readonly, weak) NSArray *visibleViews;
@property (nonatomic) NSIndexPath *selectedIndexPath;
@property (nonatomic) UIColor *cellBackgroundColor;
@property (nonatomic) EasyTableViewOrientation orientation;
@property (nonatomic, assign) CGPoint contentOffset;
Expand Down
39 changes: 3 additions & 36 deletions Classes/EasyTableView.m
Expand Up @@ -125,29 +125,6 @@ - (CGSize)contentSize {
return size;
}

#pragma mark - Selection

- (void)setSelectedIndexPath:(NSIndexPath *)indexPath {
if (![_selectedIndexPath isEqual:indexPath]) {
NSIndexPath *oldIndexPath = [_selectedIndexPath copy];

_selectedIndexPath = indexPath;

UITableViewCell *deselectedCell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:oldIndexPath];
UITableViewCell *selectedCell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:_selectedIndexPath];

if ([self.delegate respondsToSelector:@selector(easyTableView:selectedView:atIndexPath:deselectedView:)]) {
UIView *selectedView = [selectedCell viewWithTag:CELL_CONTENT_TAG];
UIView *deselectedView = [deselectedCell viewWithTag:CELL_CONTENT_TAG];

[self.delegate easyTableView:self
selectedView:selectedView
atIndexPath:_selectedIndexPath
deselectedView:deselectedView];
}
}
}

#pragma mark - Multiple Sections

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {
Expand Down Expand Up @@ -249,28 +226,18 @@ - (CGPoint)offsetForView:(UIView *)view {
#pragma mark - TableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[self setSelectedIndexPath:indexPath];
if ([self.delegate respondsToSelector:@selector(easyTableView:didSelectRowAtIndexPath:)]) {
[self.delegate easyTableView:self didSelectRowAtIndexPath:indexPath];
}
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.delegate respondsToSelector:@selector(easyTableView:heightOrWidthForCellAtIndexPath:)]) {
return [self.delegate easyTableView:self heightOrWidthForCellAtIndexPath:indexPath];
}
return _cellWidthOrHeight;
}


- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Don't allow the currently selected cell to be selectable
if ([_selectedIndexPath isEqual:indexPath]) {
return nil;
}
return indexPath;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ([self.delegate respondsToSelector:@selector(easyTableView:scrolledToOffset:)])
[self.delegate easyTableView:self scrolledToOffset:self.contentOffset];
Expand Down
28 changes: 21 additions & 7 deletions Classes/EasyTableViewController.m
Expand Up @@ -28,6 +28,10 @@
#endif

@implementation EasyTableViewController
{
NSIndexPath * _selectedVerticalIndexPath;
NSIndexPath * _selectedHorizontalIndexPath;
}

- (void)viewDidLoad {
[super viewDidLoad];
Expand Down Expand Up @@ -134,19 +138,29 @@ - (void)easyTableView:(EasyTableView *)easyTableView setDataForView:(UIView *)vi
label.text = [NSString stringWithFormat:@"%i", indexPath.row];

// selectedIndexPath can be nil so we need to test for that condition
BOOL isSelected = (easyTableView.selectedIndexPath) ? ([easyTableView.selectedIndexPath compare:indexPath] == NSOrderedSame) : NO;
NSIndexPath * selectedIndexPath = (easyTableView == self.verticalView) ? _selectedVerticalIndexPath : _selectedHorizontalIndexPath;
BOOL isSelected = selectedIndexPath ? ([selectedIndexPath compare:indexPath] == NSOrderedSame) : NO;
[self borderIsSelected:isSelected forView:view];
}

// Optional delegate to track the selection of a particular cell

- (void)easyTableView:(EasyTableView *)easyTableView selectedView:(UIView *)selectedView atIndexPath:(NSIndexPath *)indexPath deselectedView:(UIView *)deselectedView {
[self borderIsSelected:YES forView:selectedView];

if (deselectedView)
[self borderIsSelected:NO forView:deselectedView];
- (UIView *)viewForIndexPath:(NSIndexPath *)indexPath easyTableView:(EasyTableView *)tableView
{
UITableViewCell * cell = [tableView.tableView cellForRowAtIndexPath:indexPath];
return [cell viewWithTag:CELL_CONTENT_TAG];
}

- (void)easyTableView:(EasyTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *__strong* selectedIndexPath = (tableView == self.verticalView) ? &_selectedVerticalIndexPath : &_selectedHorizontalIndexPath;

if (selectedIndexPath)
[self borderIsSelected:NO forView:[self viewForIndexPath:*selectedIndexPath easyTableView:tableView]];

*selectedIndexPath = indexPath;
UILabel * label = (UILabel *)[self viewForIndexPath:*selectedIndexPath easyTableView:tableView];
[self borderIsSelected:YES forView:label];

UILabel *label = (UILabel *)selectedView;
self.bigLabel.text = label.text;
}

Expand Down

0 comments on commit 7f0272c

Please sign in to comment.