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
  • 25,505
  • 44
  • 151
  • 277

4 Answers4

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];
andyroberts
  • 3,458
  • 2
  • 37
  • 40
  • 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. – andyroberts 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
  • 3,324
  • 1
  • 26
  • 27
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
  • 132,869
  • 46
  • 340
  • 423
0

If you're trying to present a ÙITableViewController, calling tableView.reloadData() in viewDidLoad() or viewWillAppear() will cause noticeable UI lag before the presentation animation.

However, you will need to wait for the ÙITableView to have loaded its data and layed out its view, otherwise scrollToRow(at:, at:, animated:) will not work.

Here's the (slightly hacky) solution I came with up with for scrolling in a table view which is currently being presented:

private var needsToScroll = true

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    
    if needsToScroll {
        needsToScroll = false
        DispatchQueue.main.async {
            let indexPath = ...
            tableView.scrollToRow(at: indexPath, at: .top, animated: false)
        }
    }
}

needsToScroll is needed because viewDidLayoutSubviews() will often be called more than once, which is an issue since scrollToRow(at:, at:, animated:) can result in viewDidLayoutSubviews() being called again.

YourMJK
  • 1,429
  • 1
  • 11
  • 22