Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

How to access labels in a row when a switch is touched

Hi, I've been looking for info on something I'm sure is simple.
I have a table with rows gathered via RSS. Each row has two labels and a switch. When the switch is touched the event returns the switch object but I can't get the value of a label from the same row.

Any assistance would be great :)

            var codeSwitch = Titanium.UI.createSwitch({
                value:found,
                right:1
            });
            codeSwitch.addEventListener('change',function(e)
            {
                Titanium.API.info('The text in the label is = ' + e.???.label.text);
}
— asked December 6th 2010 by Tristran Gordon
  • parent
  • row
0 Comments

6 Answers

  • For anyone else trying to do this there's more or less no sane way of doing this with documented APIs but if you feel like taking a chance that the undocumented ones will work you can try this:

    var indexOfLabelInRow = 0; // the index of the label you want to show in the row
    var codeSwitch = Titanium.UI.createSwitch({
                    value:found,
                    right:1
                });
                codeSwitch.addEventListener('change',function(e) {
                    var row = e.source.getParent();
                    Titanium.API.info('The text in the label is = ' + row.children[indexOfLabelInRow].text);
                }
    

    AFAIK neither the getParent() method or the children properties of views is documented and as such your milage may vary

    — answered July 5th 2011 by Karl Åström
    permalink
    1 Comment
    • thanks it's working.

      — commented January 17th 2013 by Nick Yang
  • Tristran

    You haven't stated your mobile platform or Ti SDK version. The easiest way to do this is to simply add the information to the tags, when you post the question.

    tableViewRow has a children property, which is an array of Ti objects. They are numbered 0 to n-1, in the same order you added them using the add() method.

    Hope this helps

    — answered December 6th 2010 by Paul Dowsett
    permalink
    1 Comment
    • Hi thanks for your comment :)
      The platform is Android under SDK API 1.6

      In this instance there is a switch on each row, and I want to get the label of the row after a switch event. I don't think the label is a child of the switch? I would expect to need to get the switches parent (the row) then the child of that (the label). I hope that makes sense. Sorry for noob questions I'm still getting started with Titanium.

      Cheers

      — commented December 6th 2010 by Tristran Gordon
  • If anyone can help with this it would be great, it's got me completely stumped.

    — answered December 7th 2010 by Tristran Gordon
    permalink
    0 Comments
  • I believe - as Hal H - was saying to grab your label you need to do this:

    yourLabel = e.row.children[n];

    Where n is the number of your label in the 'children' array

    — answered December 7th 2010 by Patrick Mounteney
    permalink
    0 Comments
  • Hi, the thing I'm stumbling on is that e.row is not an event property of the switch object. I need to get the parent of the switch, then the child. The switch has no children, and also no row since it isn't a tableview.

    When I print e.row it is undefined.

    — answered December 8th 2010 by Tristran Gordon
    permalink
    0 Comments
  • Tristan - Ah, I see. You are trying to trap the 'change' from the switch (which is logical) but there seems to be a huge problem with Appcelerator in that the recommended way of trapping events in a table is to listen for a 'click' event on the table, not a row or anything in the row.

    If you put all your switches in an array you would then have to find your switch by something like this:

    function tableClickHandler(e){
    var theSwitch = yourArrayOfSwitchObjects[e.index]
    }
    

    Hope this helps a bit…

    — answered December 12th 2010 by Patrick Mounteney
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.