Select row in TableView

  1. 5 years ago

    TheMexican

    15 Jul 2018 User since 2016

    I have been struggling to select a row programmatically in a TableView. I want to select the second row when the window shows.

    What I can get from the documentation is that to select a row you can use the Tableview.selectRow(indexPath,animated,ScrollPosition)

    so I have the following code in the Window's DidShow event:

    var secRow = TableView1.indexPathForRowAtPoint(Point(1,0));
    Console.write("index Path = \(secRow)");
    
    TableView1.selectRow(secRow, true, 0);    //Gives me an error because secRow is null

    var secRow is always null

    I have tried to change the Point(0,1) but same result.
    Please Advise

    I am attaching project if that helps.

  2. TheMexican

    15 Jul 2018 User since 2016

    Here is a screenshot

  3. doobox

    15 Jul 2018 User since 2018

    TableView1.selectRow(IndexPath(1,0), true, TableViewScrollPosition.Top)

    in windows DidShow event.

  4. doobox

    15 Jul 2018 User since 2018
    Edited 5 years ago by doobox

    I'm not sure, as I haven't tested. But I imagine the way you were trying using:

    var secRow = TableView1.indexPathForRowAtPoint(Point(1,0));

    Is looking for a row that that point falls within its bounds.
    Kind of like "give me the colour of the pixel at point(x,y)".
    I guess a use case for that would be if select cells was disabled you could use the coords of the touch event for the point.
    Just a guess.

  5. marco

    16 Jul 2018 Administrator User since 2016

    @TheMexican, @doobox is right, the best way to programmatically select a row is directly using an IndexPath instead of a Point conversion.

    In your Window1.DidShow event the code should look like:

    var secRow = IndexPath(1, 0);
    TableView1.selectRow(secRow, true, 0);

    I attached a modified project.
    P.S. please note that there is a graphic selection issue, we'll fix it in the next upcoming release.

  6. TheMexican

    18 Jul 2018 User since 2016

    Thank you @marco , I got confused because the documentation says
    "super: Object

    This class is reserved and cannot be directly instantiated. The NSIndexPath class represents the path to a specific node in a tree of nested array collections. Each index in an index path represents the index into an array of children from one node in the tree to another, deeper, node."

    I am still learning the difference between initializing using a function and creating an instance.

or Sign Up to reply!