Skip to content

Commit b7ee6ea

Browse files
Merge pull request #1103 from harleyjcooper/harley/kif-fix-table-scrolling
Fixing broken cell scrolling in TableViews
2 parents 3f1fd2c + 9bc48ea commit b7ee6ea

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

Additions/UIView-KIFAdditions.m

+8-18
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ - (UIAccessibilityElement *)accessibilityElementMatchingBlock:(BOOL(^)(UIAccessi
242242
}
243243

244244
UITableView *tableView = (UITableView *)self;
245-
__block NSIndexPath *firstIndexPath = nil;
245+
CGRect initialPosition = CGRectMake(tableView.contentOffset.x, tableView.contentOffset.y, tableView.frame.size.width, tableView.frame.size.height);
246246

247247
// Because of a bug in [UITableView indexPathsForVisibleRows] http://openradar.appspot.com/radar?id=5191284490764288
248248
// We use [UITableView visibleCells] to determine the index path of the visible cells
@@ -252,13 +252,9 @@ - (UIAccessibilityElement *)accessibilityElementMatchingBlock:(BOOL(^)(UIAccessi
252252
if (indexPath) {
253253
[indexPathsForVisibleRows addObject:indexPath];
254254
}
255-
if (!firstIndexPath || ([firstIndexPath compare:indexPath] == NSOrderedDescending)) {
256-
firstIndexPath = indexPath;
257-
}
258255
}];
259256

260-
BOOL animationEnabled = [KIFUITestActor testActorAnimationsEnabled];
261-
CFTimeInterval delay = animationEnabled ? 0.5 : 0.05;
257+
CFTimeInterval delay = 0.05;
262258
for (NSUInteger section = 0, numberOfSections = [tableView numberOfSections]; section < numberOfSections; section++) {
263259
for (NSUInteger row = 0, numberOfRows = [tableView numberOfRowsInSection:section]; row < numberOfRows; row++) {
264260
if (!self.window) {
@@ -270,21 +266,15 @@ - (UIAccessibilityElement *)accessibilityElementMatchingBlock:(BOOL(^)(UIAccessi
270266
if ([indexPathsForVisibleRows containsObject:indexPath]) {
271267
@autoreleasepool {
272268
//scroll to the last row of each section before continuing. Attemps to ensure we can get to sections that are off screen. KIF tests (e.g. testButtonAbsentAfterRemoveFromSuperview) fails without this line. Also without this... we can't expose the next section (in code downstream)
273-
[tableView scrollToRowAtIndexPath:[indexPathsForVisibleRows lastObject] atScrollPosition:UITableViewScrollPositionNone animated:animationEnabled];
269+
[tableView scrollToRowAtIndexPath:[indexPathsForVisibleRows lastObject] atScrollPosition:UITableViewScrollPositionNone animated:NO];
274270
continue;
275271
}
276272
}
277273

278-
//expose the next section (unless it's a UIPicker View).
279-
if (subViewName && ![subViewName containsString:@"UIPicker"] )
280-
{
281-
CGRect sectionRect = [tableView rectForSection:section];
282-
[tableView scrollRectToVisible:sectionRect animated:NO];
283-
}
284-
285274
@autoreleasepool {
286275
// Scroll to the cell and wait for the animation to complete. Using animations here may not be optimal.
287-
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:animationEnabled];
276+
CGRect sectionRect = [tableView rectForRowAtIndexPath:indexPath];
277+
[tableView scrollRectToVisible:sectionRect animated:NO];
288278

289279
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
290280
UIAccessibilityElement *element = [cell accessibilityElementMatchingBlock:matchBlock notHidden:NO];
@@ -300,13 +290,13 @@ - (UIAccessibilityElement *)accessibilityElementMatchingBlock:(BOOL(^)(UIAccessi
300290
return [self accessibilityElementMatchingBlock:matchBlock];
301291
}
302292
}
303-
if (firstIndexPath) {
304-
[tableView scrollToRowAtIndexPath:firstIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
305-
}
293+
306294
//if we're in a picker (scrollView), let's make sure we set the position back to how it was last set.
307295
if(scrollView != nil && scrollContentOffset.x != -1.0)
308296
{
309297
[scrollView setContentOffset:scrollContentOffset];
298+
} else {
299+
[tableView scrollRectToVisible:initialPosition animated:NO];
310300
}
311301
CFRunLoopRunInMode(UIApplicationCurrentRunMode, delay, false);
312302
} else if ([self isKindOfClass:[UICollectionView class]]) {

0 commit comments

Comments
 (0)