Skip to content

Commit be9b8b6

Browse files
committed
Intelligent table scrolling has been added
If any dropdown expanded cell is not visible when expanding, library will scroll the table to show it.
1 parent a85c877 commit be9b8b6

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

VPPDropDown/VPPDropDown.m

+17-6
Original file line numberDiff line numberDiff line change
@@ -481,14 +481,14 @@ - (void) toggleDropDown {
481481
[VPPDropDown addNumberOfRows:rowsToAdd forSection:self.indexPath.section inTableView:self.tableView];
482482
[self updateGlobalIndexPaths];
483483

484-
NSMutableArray *indexPaths = [NSMutableArray array];
485-
for (int i = 1; i <= [_elements count]; i++) {
486-
NSIndexPath *ip = [NSIndexPath indexPathForRow:_rootIndexPath.row+i inSection:_rootIndexPath.section];
487-
[indexPaths addObject:ip];
488-
}
489-
490484
if (self.usesEntireSection) {
491485
// we can add or remove the cells as we manage the entire section
486+
NSMutableArray *indexPaths = [NSMutableArray array];
487+
for (int i = 1; i <= [_elements count]; i++) {
488+
NSIndexPath *ip = [NSIndexPath indexPathForRow:_rootIndexPath.row+i inSection:_rootIndexPath.section];
489+
[indexPaths addObject:ip];
490+
}
491+
492492
if (_expanded) {
493493
// table view insert rows
494494
[_tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
@@ -507,6 +507,17 @@ - (void) toggleDropDown {
507507
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:_rootIndexPath.section];
508508
[_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
509509
}
510+
511+
if (_expanded) {
512+
// scroll to last cell if needed
513+
NSIndexPath *lastRow = [NSIndexPath indexPathForRow:_globalRootIndexPath.row+[_elements count] inSection:_globalRootIndexPath.section];
514+
UITableViewCell *lastVisibleCell = [self.tableView.visibleCells lastObject];
515+
NSIndexPath *lastVisibleIndexPath = [self.tableView indexPathForCell:lastVisibleCell];
516+
// lets scroll if only half of the cells are visible
517+
if (lastVisibleIndexPath.section <= lastRow.section && lastVisibleIndexPath.row < lastRow.row) {
518+
[self.tableView scrollToRowAtIndexPath:lastRow atScrollPosition:UITableViewScrollPositionBottom animated:YES];
519+
}
520+
}
510521
}
511522

512523

VPPDropDownExample/DropDownExample.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ - (id)initWithStyle:(UITableViewStyle)style
5858
tableView:self.tableView
5959
indexPath:[NSIndexPath indexPathForRow:kRowDropDownDisclosure inSection:kSection1]
6060
delegate:self
61-
elementTitles:@"Disclosure 1", @"Disclosure 2", @"Disclosure 3", nil];
61+
elementTitles:@"Disclosure 1", @"Disclosure 2", @"Disclosure 3", @"Disclosure 4", @"Disclosure 5", nil];
6262

6363

6464
NSMutableArray *elts = [NSMutableArray array];

0 commit comments

Comments
 (0)