【问题标题】:Error while trying to append a row to a table titanium尝试将行附加到表钛时出错
【发布时间】:2015-07-31 14:04:10
【问题描述】:

我正在尝试向表中添加一行。该表是用合金创建的,我正在尝试从 js 中追加行。

这里是xml

<Alloy>
<Window id='index' class="container">
    <TableView  id="MainThings">
        <TableViewSection id='MainThingsSection'>

            </TableViewSection>
    </TableView>
    <Label id="AddCounter" onClick="doClick">+</Label>
    <Label id="clear" onClick="clear">-</Label>
</Window>

这是.js

function Loader(){
var row= Titanium.UI.createTableViewRow({
    title:'Title'
});
$.MainThingsSection.append(row);
}$.MainThings.addEventListener('open',Loader());
$.index.open();

这是错误

[ERROR] :  Script Error {
[ERROR] :      backtrace = "#0 () at file:///Users/stephenhanrahan/Library/Developer/CoreSimulator/Devices/C088B99B-2086-4FA3-AABD-85E2B4BE3944/data/Containers/Bundle/Application/A8E9395F-44CF-4699-92EC-9638E2473142/I%20Have%20This%20Many.app/alloy.js:265";
[ERROR] :      line = 93;
[ERROR] :      message = "Invalid type passed to function";
[ERROR] :      nativeLocation = "-[TiProxy addEventListener:] (TiProxy.m:824)";
[ERROR] :      nativeReason = "expected: Function, was: NSNull";
[ERROR] :      sourceId = 286306016;
[ERROR] :      sourceURL = "file:///Users/stephenhanrahan/Library/Developer/CoreSimulator/Devices/C088B99B-2086-4FA3-AABD-85E2B4BE3944/data/Containers/Bundle/Application/A8E9395F-44CF-4699-92EC-9638E2473142/I%20Have%20This%20Many.app/alloy/controllers/index.js";
[ERROR] :  }

【问题讨论】:

    标签: javascript uitableview appcelerator titanium-alloy


    【解决方案1】:

    此代码中的错误是它不喜欢该行。:

    $.MainThings.addEventListener('open',Loader()); 
    Should Be
    $.MainThings.addEventListener('open',Loader); 
    

    除了没有“打开”事件的 TableView 之外,此代码不会按照您尝试的方式链接事件。

    index.xml

    <Alloy>
        <Window id='index' class="container">
            <TableView  id="MainThings">
                <TableViewSection id='MainThingsSection'>
    
                </TableViewSection>
            </TableView>
            <!-- <Label id="AddCounter" onClick="doClick">+</Label> -->
            <!-- <Label id="clear" onClick="clear">-</Label> -->
        </Window>
    </Alloy>
    

    index.js

    function Loader(){
        console.log("This code doesn't run."); // <====== This doesn't run.
        var row = Ti.UI.createTableViewRow({
            title:'Title'
        });
        $.MainThingsSection.add(row);
    }
    
    $.MainThings.addEventListener('open', Loader); // <===== This is nothing.
    $.index.open();
    

    【讨论】:

      【解决方案2】:

      没有必要使用 $.MainThings.addEventListener('open',Loader()); 代码。而是可以在 $.index.open() 之前直接调用 Loader() 函数。

      而不是使用 append 方法使用 appendRow 方法

      $.MainThingsSection.appendRow(row);
      

      注意: 在表格渲染之前,可以使用 TableViewSection add 方法将 TableViewRow 对象添加到一个部分。渲染后,必须使用 TableView 的 insertRowBefore、insertRowAfter 或 appendRow 方法之一。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-01-18
        • 1970-01-01
        • 1970-01-01
        • 2017-05-04
        • 1970-01-01
        • 2011-07-22
        • 2018-09-04
        相关资源
        最近更新 更多