42

I want to make next:

I have an UITableView that have to dispaly words (A-Z).

Currently when view did load I have one cell that displayed (and this is correct). First cell display first word from my array words.

Purpose: I want to move to the cell that must display 10 word from my array, but problem is that the cell with indexPath.row = 10 does not exist (and this correct, because I don't scroll yet).

What is a right wait to make transition from 1 to 10 cell.

I think if I don't use dequeueReusableCellWithIdentifier for creating my cell I can do it and solve my problem, but I mean this problem for device memory.

In other words I need to make scrollToRowAtIndexPath

Thanks!

Matrosov Oleksandr
  • 23,195
  • 44
  • 142
  • 269

3 Answers3

87

You are right to identify the scrollToRowAtIndexPath method. So all you need to do is create a fresh IndexPath with the row you wish to move to, e.g., row index = 10:

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:10 inSection:indexPath.section] 
             atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
arooaroo
  • 3,398
  • 2
  • 35
  • 39
  • thanks for response, but how can I scroll at indexPath that does not exist yet? – Matrosov Oleksandr Apr 22 '12 at 22:07
  • I now wonder if I understand your original question. As I understand your table may have lots of cells, e.g., 100, but only about 10 are visible at a time. Although there aren't technically 100 actual rows in memory (due to cell reuse), it's still fine to programatically scroll to a row that is not yet visible. E.g, row 50. – arooaroo Apr 23 '12 at 17:23
  • thanks again. now I have solved this problem, but scrollToRowAtIndexPath with animatio NO does not work yet. Therefore if animation YES everething works good. – Matrosov Oleksandr Apr 25 '12 at 16:02
  • Where shall I put the method? – Dejell Oct 07 '13 at 09:32
  • @arooaroo What should I do in a situation like the following. http://stackoverflow.com/questions/21041318/selectrowatindexpath-does-not-scroll-in-to-the-correct-position-in-uitableview – deltaaruna Jan 13 '14 at 09:10
  • @MatrosovAlexander You could also refer to [this](http://stackoverflow.com/a/26504470/1615594) to find if the indexpath exists – Taku Jun 06 '16 at 03:54
6
//For iOS 7 
[self.TableView reloadData];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:1];
[TableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
Alp Altunel
  • 2,946
  • 1
  • 22
  • 26
A R
  • 461
  • 4
  • 14
2

One can identify the row(in the eg. below it is forRow) that needs to be made visible and reload the row. Upon the completion of that one can scroll to that row.

let moveToIndexPath = NSIndexPath(forRow: forRow, inSection: 0)
self.tableView.reloadRowsAtIndexPaths([moveToIndexPath], withRowAnimation: .None)
self.tableView.scrollToRowAtIndexPath(moveToIndexPath, atScrollPosition: atScrollPosition, animated: true)
Tunaki
  • 125,519
  • 44
  • 317
  • 399