Dave
Dave

Reputation: 4048

UITableView scrolling to specific position

I am using iPhone SDK 3.1.3. I have a UITableViewController which gets data from another controller. The table view is added as a subview to the mainview but the frame is set so that it is not visible. The table view frame is updated and made to slide over the main view by tapping on a button.

The table view appears and I scroll to the last row. If I select the last row, I reload the table with more data. The table gets updated with more data. Everything works fine except the scroll position is always the top.

I need the scroll position to be the last row that I clicked on to load more data. I save the scroll position and call the following code after it loads more data. It executes without issues but the scroll position is always the top.

 [theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:savedScrollPosition animated:NO];

The above seems to have no effect. ViewWillAppear: ViewDidAppear: does not fire and I am told that if the view controller is instantiated in code, which is the case, these don't fire. Please help me figure out how and when to set the scroll position after the table is reloaded ([theTableView reloadData]) so that it is at the row that I clicked on.

Code to Reload table view & scroll

 ////performAction will notify the tableviewcontroller which will result in didPerformAction being called
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     if (indexPath.row == lastRow)
     {
       savedScrollPosition = lastRow;
       //perform the action
       [controller performAction];
     }
}

- (void) didPerformAction:(NSNotification *)obj
{
  [theTableView reloadData];
  [theTableView
     scrollToRowAtIndexPath: [NSIndexPath indexPathForRow:savedScrollPosition inSection:0]
     atScrollPosition:UITableViewScrollPositionBottom 
     animated:NO];
}

Upvotes: 8

Views: 44728

Answers (3)

Dave
Dave

Reputation: 4048

This seemed to do the trick.

[theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
CGPoint point = theTableView.contentOffset;
point .y -= theTableView.rowHeight;
theTableView.contentOffset = point;

Upvotes: 30

drawnonward
drawnonward

Reputation: 53659

It will look better and the scroll position will stay fixed if you can insert the rows instead of calling reloadData.

[theTableView beginUpdates];
[theTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
// make sure the dataSource will return new rows before calling endUpdates
[theTableView endUpdates];

Instead of using UITableView scrolling you can use UIScrollView scrolling:

savedOffset = [theTableView contentOffset];

Then restore:

[theTableView setContentOffset:savedOffset];

Upvotes: 10

user121301
user121301

Reputation:

If that's the actual code and assuming theTableView is not nil there, you should be getting a warning saying "may not respond to..." because scrollToRowAtIndexPath is spelled wrong.

Secondly, the atScrollPosition parameter expects a UITableViewScrollPosition enum value indicating where on the screen you want the target row to be positioned.

Try this:

[theTableView scrollToRowAtIndexPath:
                [NSIndexPath indexPathForRow:savedScrollPosition inSection:0]
                atScrollPosition:UITableViewScrollPositionBottom 
                animated:NO];

Upvotes: 0

Related Questions