From e1ac914dc589abda8332b76214244924534976a1 Mon Sep 17 00:00:00 2001 From: Chris Dunkel Date: Wed, 24 Oct 2018 17:36:23 -0400 Subject: [PATCH 1/5] feat: Exposes functions for programmatically showing/hiding the menu, as well as getting the current visibility state of the menu. --- Classes/PFNavigationDropdownMenu.h | 6 ++++++ Classes/PFNavigationDropdownMenu.m | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/Classes/PFNavigationDropdownMenu.h b/Classes/PFNavigationDropdownMenu.h index 84319f9..c4e3175 100644 --- a/Classes/PFNavigationDropdownMenu.h +++ b/Classes/PFNavigationDropdownMenu.h @@ -26,4 +26,10 @@ title:(NSString *)title items:(NSArray *)items containerView:(UIView *)containerView; + +// Open/Close State +- (void)openMenu; +- (void)closeMenu; +- (BOOL)isOpen; + @end diff --git a/Classes/PFNavigationDropdownMenu.m b/Classes/PFNavigationDropdownMenu.m index 74ff0f8..5c7d2d3 100644 --- a/Classes/PFNavigationDropdownMenu.m +++ b/Classes/PFNavigationDropdownMenu.m @@ -195,6 +195,24 @@ - (void)menuButtonTapped:(UIButton *)sender } } +#pragma mark - Open/Close State +- (void)openMenu +{ + self.isShown = YES; + [self showMenu]; +} + +- (void)closeMenu +{ + self.isShown = NO; + [self hideMenu]; +} + +- (BOOL)isOpen +{ + return [self isShown]; +} + #pragma mark - Setters - (void)setCellHeight:(CGFloat)cellHeight { From 162a2ab416bfc5c8029a034990d91efed78fbeb5 Mon Sep 17 00:00:00 2001 From: Chris Dunkel Date: Thu, 25 Oct 2018 12:44:18 -0400 Subject: [PATCH 2/5] feat: Adds the ability to set the selected index programmatically. --- Classes/PFNavigationDropdownMenu.h | 3 +++ Classes/PFNavigationDropdownMenu.m | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/Classes/PFNavigationDropdownMenu.h b/Classes/PFNavigationDropdownMenu.h index c4e3175..61ea933 100644 --- a/Classes/PFNavigationDropdownMenu.h +++ b/Classes/PFNavigationDropdownMenu.h @@ -32,4 +32,7 @@ - (void)closeMenu; - (BOOL)isOpen; +// Item Selection +- (void)selectItemAtIndex:(NSUInteger)index; + @end diff --git a/Classes/PFNavigationDropdownMenu.m b/Classes/PFNavigationDropdownMenu.m index 5c7d2d3..738f8f3 100644 --- a/Classes/PFNavigationDropdownMenu.m +++ b/Classes/PFNavigationDropdownMenu.m @@ -213,6 +213,14 @@ - (BOOL)isOpen return [self isShown]; } +#pragma mark - Item Selection +- (void)selectItemAtIndex:(NSUInteger)index +{ + [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathWithIndex:index] + animated:NO + scrollPosition:UITableViewScrollPositionNone]; +} + #pragma mark - Setters - (void)setCellHeight:(CGFloat)cellHeight { From 8b05c0b9e0d5023fdaee07507ccd14af8688896c Mon Sep 17 00:00:00 2001 From: Chris Dunkel Date: Thu, 25 Oct 2018 12:56:53 -0400 Subject: [PATCH 3/5] fix: Fix issue with NSIndexPath creation. --- Classes/PFNavigationDropdownMenu.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/PFNavigationDropdownMenu.m b/Classes/PFNavigationDropdownMenu.m index 738f8f3..d9e471c 100644 --- a/Classes/PFNavigationDropdownMenu.m +++ b/Classes/PFNavigationDropdownMenu.m @@ -216,7 +216,7 @@ - (BOOL)isOpen #pragma mark - Item Selection - (void)selectItemAtIndex:(NSUInteger)index { - [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathWithIndex:index] + [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone]; } From c75cce0eda7ec92b9023c477013d12b1967e6277 Mon Sep 17 00:00:00 2001 From: Chris Dunkel Date: Thu, 25 Oct 2018 13:35:27 -0400 Subject: [PATCH 4/5] feat: Adds a way to programmatically select an index. For real this time. --- Classes/PFNavigationDropdownMenu.m | 7 ++++--- Classes/PFTableView.h | 4 ++++ Classes/PFTableView.m | 12 +++++++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Classes/PFNavigationDropdownMenu.m b/Classes/PFNavigationDropdownMenu.m index d9e471c..4a35813 100644 --- a/Classes/PFNavigationDropdownMenu.m +++ b/Classes/PFNavigationDropdownMenu.m @@ -216,9 +216,10 @@ - (BOOL)isOpen #pragma mark - Item Selection - (void)selectItemAtIndex:(NSUInteger)index { - [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] - animated:NO - scrollPosition:UITableViewScrollPositionNone]; + if (index >= self.items.count) { + index = self.items.count - 1; + } + [self.tableView selectIndex:index]; } #pragma mark - Setters diff --git a/Classes/PFTableView.h b/Classes/PFTableView.h index bc75f45..5bd26ed 100644 --- a/Classes/PFTableView.h +++ b/Classes/PFTableView.h @@ -14,4 +14,8 @@ @property (nonatomic, copy) void(^selectRowAtIndexPathHandler)(NSUInteger indexPath); - (instancetype)initWithFrame:(CGRect)frame items:(NSArray *)items configuration:(PFConfiguration *)configuration; + +// Setters +- (void)selectIndex:(NSUInteger)index; + @end diff --git a/Classes/PFTableView.m b/Classes/PFTableView.m index 1584c69..d4feab9 100644 --- a/Classes/PFTableView.m +++ b/Classes/PFTableView.m @@ -35,6 +35,14 @@ - (instancetype)initWithFrame:(CGRect)frame items:(NSArray *)items configuration return self; } +#pragma mark - Setters +- (void)selectIndex:(NSUInteger)index +{ + self.selectedIndexPath = index; + self.selectRowAtIndexPathHandler(index); + [self reloadData]; +} + #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { @@ -73,9 +81,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { - self.selectedIndexPath = indexPath.row; - self.selectRowAtIndexPathHandler(indexPath.row); - [self reloadData]; + [self selectIndex:indexPath.row]; PFTableViewCell *cell = (PFTableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; cell.contentView.backgroundColor = self.configuration.cellSelectionColor; } From 59ec0c938e3bd0f42e6835c5d27161a02b604924 Mon Sep 17 00:00:00 2001 From: Chris Dunkel Date: Thu, 25 Oct 2018 13:49:51 -0400 Subject: [PATCH 5/5] fix: Fixes an issue where if `selectItemAtIndex:` were called before setting a `didSelectItemAtIndexHandler` then an exception would be thrown. Now the handler just won't be called if it hasn't been set. --- Classes/PFNavigationDropdownMenu.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Classes/PFNavigationDropdownMenu.m b/Classes/PFNavigationDropdownMenu.m index 4a35813..3c5f92c 100644 --- a/Classes/PFNavigationDropdownMenu.m +++ b/Classes/PFNavigationDropdownMenu.m @@ -65,7 +65,10 @@ - (instancetype)initWithFrame:(CGRect)frame __weak typeof(self) weakSelf = self; self.tableView.selectRowAtIndexPathHandler = ^(NSUInteger indexPath){ __strong typeof(weakSelf) strongSelf = weakSelf; - strongSelf.didSelectItemAtIndexHandler(indexPath); + if (strongSelf.didSelectItemAtIndexHandler) + { + strongSelf.didSelectItemAtIndexHandler(indexPath); + } [strongSelf setMenuTitleText:items[indexPath]]; [strongSelf hideMenu]; strongSelf.isShown = NO;