Original Title: Scroll to specific UITableViewCell in a UITableView with dynamic multiple sections
New title: Obtain an NSIndexPath for an "off-screen" UITableViewCell from the index of a data source array
I want to be able to smoothly scroll a table view until a previously selected cell is shown on screen.
The problem arises because I cannot find a solution to determine the indexPath
of a cell before it is loaded by tableview:cellForRowAtIndexPath:
.
The UITableViewController
data source is a NSFetchedResultsController
that returns multiple sections.
I have read these SO questions, however they all provide a solution to the problem with only one section:
- How to get a UITableViewCell from out of view and scroll it into view,
- Setting scroll position in UITableView.
I have included code below - it works - however the scroll motion is uneven (start/stop) - the code scrolls a row, assesses whether the next row contains the data related to a previously selected cell, and if not scrolls to the next row, and so on and so on... ... until the previously selected cell is found and is on screen.
This code does not work on a device running iOS6.
Anyone have any ideas about how to scroll to the previously selected cell smoothly under iOS 7, and even better if the solution can work under iOS 6.
UPDATE:
- New question title,
- thanks to @dasblinkenlight, now working on determining how to obtain an NSIndexPath for an "off-screen" UITableViewCell from the index of a data source array.
Using the enumeration option identified in the first SO question link above.
Updated code to be posted...
Existing Code
Properties.
@property (nonatomic, strong) NSIndexPath *indexPathCurrent;
@property (nonatomic) BOOL selectedCellIsVisibleOnScreen;
The "action" code sits at the end of my tableview:cellForRowAtIndexPath:
method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
<... code to configure cell...>
BOOL isChecked = NO;
<... code to set the value for isChecked...>
if (self.isIOS7 && !self.selectedCellIsVisibleOnScreen) {
[self setIndexPathCurrent:indexPath];
[self setSelectedCellIsVisibleOnScreen:isChecked];
[self.tableView scrollToRowAtIndexPath:self.indexPathCurrent
atScrollPosition:UITableViewScrollPositionNone
animated:YES];
}
}
The property isIOS7
is determined elsewhere and is in place to disable this action for any device running iOS 6, as this code causes hectic behaviour in the table view when executed under iOS 6.
New Code
to be advised...