diff --git a/Classes/PFNavigationDropdownMenu.h b/Classes/PFNavigationDropdownMenu.h index 84319f9..61ea933 100644 --- a/Classes/PFNavigationDropdownMenu.h +++ b/Classes/PFNavigationDropdownMenu.h @@ -26,4 +26,13 @@ title:(NSString *)title items:(NSArray *)items containerView:(UIView *)containerView; + +// Open/Close State +- (void)openMenu; +- (void)closeMenu; +- (BOOL)isOpen; + +// Item Selection +- (void)selectItemAtIndex:(NSUInteger)index; + @end diff --git a/Classes/PFNavigationDropdownMenu.m b/Classes/PFNavigationDropdownMenu.m index 74ff0f8..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; @@ -195,6 +198,33 @@ - (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 - Item Selection +- (void)selectItemAtIndex:(NSUInteger)index +{ + if (index >= self.items.count) { + index = self.items.count - 1; + } + [self.tableView selectIndex:index]; +} + #pragma mark - Setters - (void)setCellHeight:(CGFloat)cellHeight { 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; }