Selecting Table View Items

Applies to TestComplete 15.48, last modified on March 01, 2023

One of the most frequent actions you perform on a table view bar is selecting individual items. This topic explains various approaches that can be used to select items in scripts:

Touching Items

The iOS TableView object, which is used to work with table views, provides a number of methods for simulating a touch on items: TouchItem, LongTouchItem, TouchItemXY and LongTouchItemXY are used to simulate touches on items.

All of these actions have parameters that specify the item to be touched. To specify the item, use a section name or index and an index of an item. Note that it is possible to use wildcards (* and ?) or regular expressions to specify a section’s caption. An asterisk (*) corresponds to a string of any length (including an empty string), a question mark corresponds to any single character (including none). If you wish to specify an asterisk (*) as part of an item’s caption, you should double it, because otherwise, it will be treated as a wildcard. To specify more complicated parts of a caption, use regular expressions. For more information, see Addressing Table View Items. Here is an example of how to select an item from any section:

tableview.TouchItem("*", 1)

The sample script below simulates a touch on the first item of the "SectionName" section and then a long touch on the third item in the same section of an application’s table view.

JavaScript, JScript

function Test()
{
  // Select the mobile device
  Mobile.SetCurrent("iPhone");
  // Obtain the TableView object
  var p = Mobile.Device().Process("SampleApp");
  var tableview = p.Window().TableView();

  // Touch items
  tableview.TouchItem("SectionName", 0);
  tableview.LongTouchItem("SectionName", 2);
}

Python

def Test():
  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the TableView object 
  p = Mobile.Device().Process("SampleApp")
  tableview = p.Window().TableView()

  # Touch items
  tableview.TouchItem("SectionName", 0)
  tableview.LongTouchItem("SectionName", 2)

VBScript

Sub Test()
  Dim p, tableview
  ' Select the mobile device
  Mobile.SetCurrent("iPhone")
  ' Obtain the TableView object
  Set p = Mobile.Device.Process("SampleApp")
  Set tableview = p.Window().TableView()

  ' Touch items
  Call tableview.TouchItem("SectionName", 0)
  Call tableview.LongTouchItem("SectionName", 2)
End Sub

DelphiScript

procedure Test();
var
  p, tableview;
begin
  // Select the mobile device
  Mobile.SetCurrent('iPhone');
  // Obtain the TableView object
  p := Mobile.Device.Process('SampleApp');
  tableview := p.Window(0).TableView(0);

  // Touch items
  tableview.TouchItem('SectionName', 0);
  tableview.LongTouchItem('SectionName', 2);
end;

C++Script, C#Script

function Test()
{
  // Select the mobile device
  Mobile["SetCurrent"]("iPhone");
  // Obtain the TableView object
  var p = Mobile["Device"]["Process"]("SampleApp");
  var tableview = p["Window"]()["TableView"]();

  // Touch items
  tableview["TouchItem"]("SectionName", 0);
  tableview["LongTouchItem"]("SectionName", 2);
}

Selecting or Unselecting Items

To select or unselect individual items in the control, you can also use the SelectItem method. To select an item, set the method’s Select parameter to true (or leave the parameter unspecified); to unselect an item - set the parameter to false. The method simulates a touch on the item and sets the item to the specified state. If the item is already in the desired state, no action is performed. The following example demonstrates how to use the SelectItem method to select an item and then to unselect it:

JavaScript, JScript

function Test()
{
  // Select the mobile device
  Mobile.SetCurrent("iPhone");
  // Obtain the TableView object
  var p = Mobile.Device().Process("SampleApp");
  var tableview = p.Window().TableView();

  // Select items
  tableview.SelectItem("SectionName", 0);
  tableview.SelectItem("SectionName", 0, false);
}

Python

def Test():
  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the TableView object 
  p = Mobile.Device().Process("SampleApp")
  tableview = p.Window().TableView()

  # Select items
  tableview.SelectItem("SectionName", 0)
  tableview.SelectItem("SectionName", 0, False)

VBScript

Sub Test()
  Dim p, tableview
  ' Select the mobile device
  Mobile.SetCurrent("iPhone")
  ' Obtain the TableView object
  Set p = Mobile.Device.Process("SampleApp")
  Set tableview = p.Window().TableView()

  ' Select items
  Call tableview.SelectItem("SectionName", 0)
  Call tableview.SelectItem("SectionName", 0, false)
End Sub

DelphiScript

procedure Test();
var
  p, tableview;
begin
  // Select the mobile device
  Mobile.SetCurrent('iPhone');
  // Obtain the TableView object
  p := Mobile.Device.Process('SampleApp');
  tableview := p.Window(0).TableView(0);

  // Select items
  tableview.SelectItem('SectionName', 0);
  tableview.SelectItem('SectionName', 0, false);
end;

C++Script, C#Script

function Test()
{
  // Select the mobile device
  Mobile["SetCurrent"]("iPhone");
  // Obtain the TableView object
  var p = Mobile["Device"]["Process"]("SampleApp");
  var tableview = p["Window"]()["TableView"]();

  // Select items
  tableview["SelectItem"]("SectionName", 0);
  tableview["SelectItem"]("SectionName", 0, false);
}

Selecting Multiple Items

You may also need to select multiple items. To do this, use the MultiSelect method and specify the items you want to select by their section’s name or index and the items’ indices. If there are items already selected in the specified sections of the object, the method first clears the selection in these sections and then selects the specified items. The item selection in other sections does not change. If the object does not support multiselection, the method performs no actions.

The following example selects the first six items in the first section:

JavaScript, JScript

function Test()
{
  // Select the mobile device
  Mobile.SetCurrent("iPhone");
  // Obtain the TableView object
  var p = Mobile.Device().Process("SampleApp");
  var tableview = p.Window().TableView();
  
  // Create an array of items you want to select
  var items = new Array(tableview.wItemCount(0))
  for (i=0; i<tableview.wItemCount(0);i++)
    items[i] = i
    
  // Select the items
  tableview.Multiselect(0, items);
}

Python

def Test():
  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the TableView object 
  p = Mobile.Device().Process("SampleApp")
  tableview = p.Window().TableView()
  
  # Create an array of items you want to select
  items = [i for i in range(tableview.wItemCount[0])]
    
  # Select the items 
  tableview.Multiselect(0, items)

VBScript

Sub Test()
  Dim p, tableview
  ' Select the mobile device
  Mobile.SetCurrent("iPhone")
  ' Obtain the TableView object
  Set p = Mobile.Device.Process("SampleApp")
  Set tableview = p.Window.TableView
  
  ' Create an array of items you want to select
  Dim items(5)
  For i = 0 to 5
    items(i) = i
  Next
  ' Select the items
  Call tableview.MultiSelect(0,items)
End Sub

DelphiScript

procedure Test();
var
  p, tableview, i;
  items : array[0..5] of variant;
begin
  // Select the mobile device
  Mobile.SetCurrent('iPhone');
  // Obtain the TableView object
  p := Mobile.Device.Process('SampleApp');
  tableview := p.Window(0).TableView(0);
  
  // Create an array of items you want to select
  for i := 0 to 5 do
    items[i] := i;
  
  // Select the items
  tableview.MultiSelect(0, items);
end;

C++Script, C#Script

function Test()
{
  // Select the mobile device
  Mobile["SetCurrent"]("iPhone");
  // Obtain the TableView object
  var p = Mobile["Device"].Process("SampleApp");
  var tableview = p["Window"]()["TableView"]();
  
  var items = new Array(tableview.wItemCount(0))
  for (i=0; i<5;i++)
    items[i] = i
  tableview["Multiselect"](0, items);
}

Simulating Actions From Keyword Tests

To select items of table views from keyword tests, call the methods described above by using the On-Screen Action or Call Object Method operation. See Calling Object Methods.

See Also

Working With iOS Table View Controls
TouchItem Method (Specific to iOS TableView Controls)
LongTouchItem Action (Specific to iOS TableView Controls)
SelectItem Action (Specific to iOS TableView Controls)
MultiSelect Method (Specific to iOS TableView Controls)

Highlight search results