在钛ios中获取tableview行中的开关ID和值

时间:2017-07-22 11:34:02

标签: ios titanium titanium-alloy

我已切换到tableview行。我希望在用户点击或更改开关时获取切换值。我已经在tableview上编写了click事件,这在android中运行良好,但在ios中它没有给出开关值。

以下是我的代码。

for (var i = 0; stateList.length; i++) {
    var tblRow = Ti.UI.createTableViewRow({
        height : Ti.UI.SIZE,
        title : stateList[i].state_name,
        layout : 'vertical',
        switch_id : stateList[i].state_id,
        selectedBackgroundColor : Alloy.CFG.colors.white,
    });
    var mainView = Ti.UI.createView({
        top : 20,
        height : Ti.UI.SIZE,
        width : Ti.UI.FILL,
        backgroundColor : '#fff'
    });
    tblRow.add(mainView);
    var rowLbl = Ti.UI.createLabel({
        text : stateList[i].state_name,
        textAlign : 'left',
        left : 15,
        right : 40,
        color : Alloy.CFG.colors.color_333399,
        font : {
            fontSize : Alloy.CFG.fonts.f_12,
            fontFamily : Alloy.CFG.font_family.calibri,
            fontWeight : Alloy.CFG.font_weight.bold
        }
    });
    stateSwitch = Ti.UI.createSwitch({
        right : 10,
        value : stateList[i].enable_disable,
    });
    mainView.add(rowLbl);
    mainView.add(stateSwitch);
    var bottom_view = Ti.UI.createView({
        top : 20,
        height : 1,
        width : Ti.UI.FILL,
        backgroundColor : Alloy.CFG.colors.color_cccccc
    });
    tblRow.add(bottom_view);
    $.tbl_sendformState.insertRowAfter(1, tblRow);
}

点击表视图后,我将获取开关ID和值,如下所示。

$.tbl_sendformState.addEventListener('click', function(e) {
    Ti.API.info('e = ' + JSON.stringify(e));
    var switch_id = e.row.switch_id;
    var switch_value = e.source.value;
    Ti.API.info('switch value after clicked stringify === ' + JSON.stringify(switch_value));
    if (switch_value == true) {
        if (switch_data.indexOf(switch_id) === -1) {
            switch_data.push(switch_id);
        }
        Ti.API.info('switch clicked stringify === ' + JSON.stringify(switch_data));
        } else {
            if (switch_data.indexOf(switch_id) != -1) {
                var i = switch_data.indexOf(switch_id);
                switch_data.splice(i, 1);
            }
            Ti.API.info('switch does not clicked stringify === ' + JSON.stringify(switch_data));
        }
    });

function openDone(e) {
    Alloy.Globals.loading.show(L("loadingmsg"), false);
    var switch_value = JSON.stringify(switch_data);
    Ti.API.info('switch_value == ' + switch_value);
    Alloy.Globals.loading.hide();

}

表格视图的xml是

<Alloy>
    <Window id="filter_s">
        <TableView id="tbl_sendformState">
            <TableViewRow backgroundColor="#fff" height="0" width="Titanium.UI.FILL"/>
            <TableViewRow backgroundColor="#fff" height="0" width="Titanium.UI.FILL"/>
            <TableViewRow  height="100">
                <Label id="bottom_lbl" color="Alloy.CFG.colors.color_333399" bottom="10"/>
            </TableViewRow>
        </TableView>
        <View id="bottom_view">
            <View id="view_clearAll" onClick="openClearAll">
                <Label id="lbl_clearAll"/>
            </View>
            <View id="view_done" onClick="openDone">
                <Button id="btn_done"/>
            </View>
        </View>
    </Window>
</Alloy>

这在android中工作正常,但在ios中,switch id不会被推入switch_data数组。

请有人帮我,我是钛的新手。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我使用Scroll View而不是表格视图对此进行了排序。在Scroll视图中,它的工作正常。

Test.xml

<Alloy>

<Window id="test" backgroundColor="#fff">

<ScrollView id="scrollView" layout="vertical" bottom="100" top="50" />

<Label onClick="lblCloseWin" backgroundColor="darkgray" text="Close Window" textAlign="Titanium.UI.TEXT_ALIGNMENT_CENTER" bottom="0" width="Titanium.UI.FILL" height="100" />

</Window>

</Alloy>

Test.js

var args = $.args;

function lblCloseWin(e) {

$.test.close();

};

function createRow(i) {

var row = Ti.UI.createView({

backgroundColor : 'white',

borderColor : '#bbb',

borderWidth : 1,

width : '100%',

height : 70,

top : 0,

left : 0

});

var lblSwitch = Ti.UI.createLabel({

text : "Switch " + (i + 1),

left : "15%",

color : "#000"

});

var switchBtn = Ti.UI.createSwitch({

id : i,

right : "15%",

value : true

});

row.add(lblSwitch);

row.add(switchBtn);

switchBtn.addEventListener('change', function(e) {

Ti.API.info('Switch chamge value ====' + e.value);

Ti.API.info('Switch chamge id ====' + e.source.id);

});

return row;

}



for (var i = 0; i <= 10; i++) {

var row = createRow(i);

$.scrollView.add(row);

}


$.test.open();