Scrolling to a row in UITableView (iOS)

Last edited on

Overview

This example demonstrates how to scroll to a specific section and row in a UITableView (to make that row visible). It also demonstrates how to check if the provided section and row indexes are valid:

def main():
    # ...

    obj = waitForObject("{type='UITableView' visible='1'}")

    # Several call variants:
    scroll_uitableview(obj, 0, 0)
    scroll_uitableview(obj, 0, 1)
    scroll_uitableview(obj, 0, 1, UITableViewScrollPositionBottom)
    scroll_uitableview(obj, 0, 1, UITableViewScrollPositionBottom, True)
    scroll_uitableview(obj, 0, 1, animated=True)

    # Trying to scroll to non existing row;
    # results in runtime error when table section contains
    # only two rows:
    # Fatal: Invalid row index for section 0: 2 (last row index of section 0: 1)
    scroll_uitableview(o, 2, 0)

UITableViewScrollPositionNone = 0
UITableViewScrollPositionTop = 1
UITableViewScrollPositionMiddle = 2
UITableViewScrollPositionBottom = 3

def scroll_uitableview(uitableview_obj, section_index, row_index, uitableview_scroll_position=UITableViewScrollPositionMiddle, animated=False):
    datasource = uitableview_obj.dataSource
    last_section_index = datasource.numberOfSectionsInTableView_(uitableview_obj) - 1
    if last_section_index < section_index:
        test.fatal("Invalid section index: %s (last section index: %s)" % (section_index, last_section_index))
        return

    section_last_row_index = datasource.tableView_numberOfRowsInSection_(uitableview_obj, section_index) - 1
    if section_last_row_index < row_index:
        test.fatal("Invalid row index for section %s: %s (last row index of section %s: %s)" % (section_index, row_index, section_index, section_last_row_index))
        return

    index_path = NSIndexPath.indexPathForRow_inSection_(row_index, section_index)
    uitableview_obj.scrollToRowAtIndexPath_atScrollPosition_animated_(index_path, uitableview_scroll_position, animated)
test.py

Other implementations

JavaScript:

function main() {
    // ...

    obj = waitForObject("{type='UITableView' visible='1'}")

    // Several call variants:
    scroll_uitableview(obj, 0, 0);
    scroll_uitableview(obj, 0, 1);
    scroll_uitableview(obj, 0, 1, UITableViewScrollPositionBottom);
    scroll_uitableview(obj, 0, 1, UITableViewScrollPositionBottom, true);

    // Trying to scroll to non existing row;
    // results in runtime error when table section contains
    // only two rows:
    // Fatal: Invalid row index for section 0: 2 (last row index of section 0: 1)
    scroll_uitableview(obj, 0, 2);
}

var UITableViewScrollPositionNone = 0;
var UITableViewScrollPositionTop = 1;
var UITableViewScrollPositionMiddle = 2;
var UITableViewScrollPositionBottom = 3;

function scroll_uitableview(uitableview_obj, section_index, row_index, uitableview_scroll_position, animated)
{
    if (uitableview_scroll_position == null) {
        uitableview_scroll_position = UITableViewScrollPositionMiddle;
    }

    if (animated == null) {
        animated = true;
    }

    var datasource = uitableview_obj.dataSource;
    var last_section_index = datasource.numberOfSectionsInTableView_(uitableview_obj) - 1;
    if (last_section_index < section_index) {
        test.fatal("Invalid section index: "+ section_index + " (last section index: "+ last_section_index + ")");
        return;
    }

    var section_last_row_index = datasource.tableView_numberOfRowsInSection_(uitableview_obj, section_index) - 1;
    if (section_last_row_index < row_index) {
        test.fatal("Invalid row index for section " + section_index + ": " + row_index + " (last row index of section " + section_index + ": " + section_last_row + ")");
        return;
    }

    var index_path = NSIndexPath.indexPathForRow_inSection_(row_index, section_index);
    uitableview_obj.scrollToRowAtIndexPath_atScrollPosition_animated_(index_path, uitableview_scroll_position, animated);
}
test.js

Related information

Accessing UITableView cells (iOS)

UITableView Class Reference

UITableViewDataSource Protocol Reference

NSIndexPath UIKit Additions

UITableViewScrollPosition