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);
}
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
-
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
-
If anyone can help with this it would be great, it's got me completely stumped.
-
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
-
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.
-
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…