Accessing UITableView cells (iOS)

Last edited on

Overview

Sometimes it is necessary to access UITableView cells by their section and row index. The following code demonstrates how this can be done.

PYTHON:

def main():
    simulator = startApplication("Contacts~iphone")
    waitForApplicationLaunch()

    o = waitForObject({"type": "UITableView", "visible": 1})
    c = get_uitableview_cell(o, 0, 0)
    test.log("cell.accessibilityLabel: %s" % c.accessibilityLabel)

def get_uitableview_cell(uitableview_obj, section_index, row_index, verbose=True):
    if verbose:
        test.log("get_uitableview_cell(): Number of sections in UITableView: %s" % uitableview_obj.numberOfSections())
        for section_index in range(uitableview_obj.numberOfSections()):
            section_rows = uitableview_obj.numberOfRowsInSection_(section_index)
            test.log("get_uitableview_cell(): Rows in section #%s: %s" % (section_index, section_rows))
    index_path = NSIndexPath.indexPathForRow_inSection_(row_index, section_index)
    data_source = uitableview_obj.dataSource
    cell = data_source.tableView_cellForRowAtIndexPath_(uitableview_obj, index_path)
    return cell
test.py

Other implementations

JavaScript:

function get_uitableview_cell(uitableview_obj, section_index, row_index, verbose) {
    if (verbose) {
        test.log("get_uitableview_cell(): Number of sections in UITableView: %s" % uitableview_obj.numberOfSections());
        for (section_index in range(uitableview_obj.numberOfSections())) {
            section_rows = uitableview_obj.numberOfRowsInSection_(section_index);
            test.log("get_uitableview_cell(): Rows in section #" + section_index + ": " + section_rows);
        }
    }
    index_path = NSIndexPath.indexPathForRow_inSection_(row_index, section_index);
    data_source = uitableview_obj.dataSource;
    cell = data_source.tableView_cellForRowAtIndexPath_(uitableview_obj, index_path)
    return cell;
}
test.js

Logging UITableView contents

With the above function it is now very easy to log the contents of a UITableView:

function main() {
    // ...

    obj = waitForObject({"type": "UITableView", "visible": 1});
    log_uitableview(obj);
}

function get_uitableview_cell(...) {
    ...
}

function log_uitableview(uitableview_obj)
{
    var section_count = uitableview_obj.numberOfSections();
    for (var section_index = 0; section_index < section_count; section_index++) {
        section_row_count = uitableview_obj.numberOfRowsInSection_(section_index);
        test.log("Rows in section " + section_index + ": " + section_row_count);
        for (var row_index = 0; row_index < section_row_count; row_index++) {
            cell = get_uitableview_cell(uitableview_obj, section_index, row_index);
            test.log("Cell at section " + section_index + ", row " + row_index + ": " + cell.text, cell);
        }
    }
}
test.js

Scrolling to a row in UITableView (iOS)

UITableView Class Reference

UITableViewDataSource Protocol Reference

NSIndexPath UIKit Additions

UITableViewScrollPosition