Disabling / enabling child controls in TableViewRow

You must Login before you can answer or comment on any questions.

I have a TableView in with rows containing controls. I'd like to be able to disable / enable all the child controls for individual rows. However when I set the enabled property of TextFields (not sure about other controls just yet) it does not seem to be honored, I can still interact with the TextField control.

Here's my code for looking through the children in the TableViewRow:

for (var c in row.children) {
    Ti.API.info('BEFORE :: row.children[c]: ' + row.children[c].toString() + '; enabled: ' + row.children[c].enabled);
    if (typeof row.children[c].enabled !== 'undefined') row.children[c].enabled = isEnabled;
    Ti.API.info('AFTER :: row.children[c]: ' + row.children[c].toString() + '; enabled: ' + row.children[c].enabled);
}
And here is the Ti.API.info output:
[INFO] BEFORE :: row.children[c]: [object TiUILabel]; enabled: undefined
[INFO] AFTER :: row.children[c]: [object TiUILabel]; enabled: undefined
[INFO] BEFORE :: row.children[c]: [object TiUITextField]; enabled: true
[INFO] AFTER :: row.children[c]: [object TiUITextField]; enabled: false
Any idea why the TextField is remaining enabled afterwards?

2 Answers

It turns out that the enabled property is ignored in controls like TextField if you call .focus(). Once .focus() is called the user is able to interact with the control regardless.

In my case I had added a click event to the parent row so the user could click anywhere in the row and the TextField would get the focus for editing. This is why disabling controls wasn't working for me. I now set an _enabled property on the row and check this before calling focus().

— answered 1 year ago by Gavin Harriss
answer permalink
2 Comments
  • Oh my.. looks like a bug to me. The control should not been activated by any method if it is disabled.

    — commented 1 year ago by Alexander Bauer

  • Bizarre - don't seem to be able to mark my own answer as the solution to this problem!?

    — commented 1 year ago by Gavin Harriss

You should probably add the controls which you want to toggle ealnable to an separate Array, like

var row = ...;
...
row._controls = ...;
its possible SDK Bug but before we Claim that TRY a clean build.

— answered 1 year ago by Alexander Bauer
answer permalink
3 Comments
  • @Alexander, thanks for the suggestions. I tried a clean build but the issue still presents itself. I tried adding the controls to a row._controls array and disabling via this, but no joy their either. Not sure why you suggest duplicating / referencing controls in this way to help?

    My next idea is to add a transparent view to the row which will hopefully prevent the user interacting with the child controls. Bit of a hack of a solution though so would prefer a more elegant approach. I'll report back if successful.

    — commented 1 year ago by Gavin Harriss

  • Because you check for the enabled property, which is only set if you define it during creation.

    — commented 1 year ago by Alexander Bauer

  • Ah, I see what you mean. I'm checking if the property is undefined before setting it. Only controls like buttons, text fields, etc. will have this property present and so I only attempt to set it for these controls. Using row.children directly saves maintaining an extra array and remembering to populate it when I make changes in the future ;)

    — commented 1 year ago by Gavin Harriss

Your Answer

Think you can help? Login to answer this question!