Search code examples
titaniumtitanium-alloypicker

Change Picker Rows after window is loaded


I'm trying to change the picker rows after the window was loaded. The picker was created in the xml file but when a row was attempted to be added to it an error is thrown (see below).

How do i add the picker rows once the picker is already added?

view.xml

<!-- other parent views above -->
<Picker id="languagePicker" selectionIndicator="true" useSpinner="true"
                                  width="Ti.Platform.displayCaps.platformWidth" right="0">
</Picker>

view.js

var languages = db.execute("SELECT * FROM language");

while(languages.isValidRow()){
    $.languagePicker.add(Ti.UI.createPickerRow({title:languages.fieldByName('language')}));
    languages.next();
}

Error Log:

[ERROR] The application has crashed with an uncaught exception 'NSInvalidArgumentException'. Reason: -[NSNull rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0x109f28af0 Stack trace: 0 CoreFoundation 0x0000000109d03e4d exceptionPreprocess + 141 1 libobjc.A.dylib 0x00000001096d5deb objc_exception_throw + 48 2 CoreFoundation 0x0000000109d0c48d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x0000000109c5990a ___forwarding_ + 970 4 CoreFoundation 0x0000000109c594b8 _CF_forwarding_prep_0 + 120 5 UIKit 0x0000000106466d9c -[UILabel _contentInsetsFromFonts] + 137 6 UIKit 0x0000000106782a84 -[_UILabelLayer updateContentInsets] + 127 7 UIKit 0x0000000106782b73 -[_UILabelLayer updateContentLayerSize] + 50 8 UIKit 0x0000000106782ce1 -[_UILabelLayer layoutSublayers] + 25 9 QuartzCore 0x000000010875fe70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 10 QuartzCore 0x000000010875fcee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 11 QuartzCore 0x0000000108754475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 12 QuartzCore 0x0000000108781c0a _ZN2CA11Transaction6commitEv + 486 13 QuartzCore 0x000000010878237c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92 14 CoreFoundation 0x0000000109c2f367 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 15 CoreFoundation 0x0000000109c2f2d7 __CFRunLoopDoObservers + 391 16 CoreFoundation 0x0000000109c24f2b __CFRunLoopRun + 1147 17 CoreFoundation 0x0000000109c24828 CFRunLoopRunSpecific + 488 18 GraphicsServices 0x000000010b04bad2 GSEventRunModal + 161 19 UIKit 0x00000001061f9610 UIApplicationMain + 171 20 ISTQB 0x0000000104320de6 main + 310 21 libdyld.dylib 0x000000010a95192d start + 1


Solution

  • The picker.add() method expects an array of PickerRow objects. This works in my app:

    var data = [],
        picker = Ti.UI.createPicker();
    data.push(
        Ti.UI.createPickerRow({
            title: 'Row 1'
        }));
    data.push(
        Ti.UI.createPickerRow({
            title: 'Row2'
        }));
    picker.add(data);
    

    See http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.Picker-method-add