0

It has been suggested that one scroll to the desired row in viewWillAppear, but this does not work with iOS 7. I have only been able to make this work in iOS 7 in the viewDidAppear callback. Unfortunately, you see the desired row scroll into view. I don't want to see any scrolling, I simply want the row to be visible when loaded. Can anyone suggest the proper way to do this in iOS 7?

3 Answers3

0

If you know the cell index then it's as simple as:

[tableView setContentOffset:CGPointMake(cellLocation.x,cellLocation.y) animated:NO];

Call that just after you load your tableView data and it will scroll to your cell being on top. There are other options as well:

[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:currentRow inSection:currentSection] animated:NO scrollPosition:UITableViewScrollPositionTop];

Use this code with whatever scrollPosition you would like and Apple takes care of the bounding to the table (whereas setting the scrolling position is all user defined, it could be out of the table's view).

EDIT:

You could surround your selecting code with a call to UIView setting no animations allowed. That has worked for me in the past with different things, but I have never tried it in viewDidLoad.

[UIView setAnimationsEnabled:NO];
//Scroll the tableview
[UIView setAnimationsEnabled:YES];
Putz1103
  • 6,211
  • 1
  • 18
  • 25
0

It probably did not work in viewWillAppear, because that table had no data at this point. Add [tableView reloadData];and it should work.

user3071962
  • 320
  • 3
  • 8
  • I am always willing and happy to learn. Could you please elaborate a bit? I am not the only one with the idea: [How to Start UITableView on the Last Cell?](http://stackoverflow.com/questions/2156614/how-to-start-uitableview-on-the-last-cell) – user3071962 Jan 23 '14 at 19:43
  • Calling reloadData forces tableView to refresh all the data it has and redraw itself. It's better to not issue that call and ask tableView to scroll to row in proper callback – mdomans Jan 24 '14 at 17:06
0

Let me get this straight: you want your table view to show a certain row at the top when the view apperas? Yes?

If so, you want:

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated

with your cell indexPath, UITableViewScrollPositionTop as scrollPosition and animated NO like so

[tableView scrollToRowAtIndexPath:myExampleindexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
mdomans
  • 1,305
  • 12
  • 18
  • This is not an answer, it is a question. Delete this and post it as a comment. – rmaddy Jan 23 '14 at 15:42
  • Sorry, saved accidentaly as I was typing. – mdomans Jan 23 '14 at 15:48
  • I specifically did as you suggest. That implementation does not work in viewWillAppear, and if you use it in viewDidAppear, you still see the row scroll into view. I am trying to have a row which is not visible after loading the table actually be visible similar to Settings > Sounds > Ringtone where a "Classic" tone is selected. The "Classic" row is displayed and one doesn't see it animate or scroll into view even though it is roughly the 20th row in the table. – user2704463 Jan 23 '14 at 16:04
  • Yes. In viewWillAppear you may not have a view internals initialized yet. Use viewDidLoad. – mdomans Jan 23 '14 at 16:12
  • Does not work in viewDidLoad. It will work in viewWillAppear if you do not allow Extend Edges. Very strange; the scrollToRow... method considers top and bottom bars as visible. In other words, it was always scrolling my row into view, the row was just under the tab bar (bottom bar). – user2704463 Jan 23 '14 at 16:52
  • That's weird, all I can say, goes against what I know about view lifecycle callbacks – mdomans Jan 24 '14 at 17:05