Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
}
like image 268
Dave Avatar asked Apr 21 '10 18:04

Dave


2 Answers

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;
like image 62
Dave Avatar answered Nov 15 '22 21:11

Dave


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];
like image 36
drawnonward Avatar answered Nov 15 '22 20:11

drawnonward