如何在Titanium中向TableViewSection追加一行?

问题描述:

我正在使用Titanium开发iPhone应用程序,需要在特定的 TableViewSection 。我无法在页面加载时执行此操作,因为它是在应用程序的整个生命周期中由用户动态完成的。文档说TableViewSection有一个 add 方法,它接受两个参数,但是我无法使它工作。这是我现有的代码:

I'm developing an iPhone application in Titanium, and need to append a row to a particular TableViewSection. I can't do this on page load, as it's done dynamically by the user throughout the lifecycle of the application. The documentation says that the TableViewSection has an add method which takes two arguments, but I can't make it work. Here's my existing code:

for(var i = 0; i <= product_count; i++){
    productsTableViewSection.add(
        Ti.UI.createTableViewRow({
            title:'Testing...'
        })
     );
}

这只是传递一个参数,导致Titanium死亡未被捕获的例外:

That is just passing one argument in, and that causes Titanium to die with an uncaught exception:

2010-04-26 16:57:18.056 MyApplication[72765:207] *** Terminating app due to uncaught 
exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in 
section 2. The number of rows contained in an existing section after the update (2) must be 
equal to the number of rows contained in that section before the update (1), plus or minus the 
number of rows inserted or deleted from that section (0 inserted, 0 deleted).'
2010-04-26 16:57:18.056 MyApplication[72765:207] Stack: (

异常看起来像做了添加行,但由于某种原因不允许这样做。因为文档说 TableViewSection 接受view和row,我尝试了以下内容:

The exception looks like it did add the row, but it's not allowed to for some reason. Since the documentation says that TableViewSection takes in "view" and "row", I tried the following:

for(var i = 0; i <= product_count; i++){
    productsTableViewSection.add(
        Ti.UI.createView({}),
        Ti.UI.createTableViewRow({
            title:'Testing...'
        })
     );
}

上面的代码不会抛出异常,但会产生 [WARN]

The above code doesn't throw the exception, but it gives a [WARN]:

[WARN] Invalid type passed to function. expected: TiUIViewProxy, 
was: TiUITableViewRowProxy in  -[TiUITableViewSectionProxy add:] (TiUITableViewSectionProxy.m:62)

TableViewSections似乎不支持任何方法,如 appendRow ,或 insertRow ,所以我不支持知道在哪里可以用它。我查看了KitchenSink应用程序,但没有找到向TableViewSection添加行的示例。任何帮助都表示赞赏。

TableViewSections don't seem to support any methods like appendRow, or insertRow, so I don't know where else to go with this. I've looked through the KitchenSink app, but there are no examples that I could find of adding a row to a TableViewSection. Any help is appreciated.

我自己也在解决这个问题,经过大量的反复试验后我发现了这个问题TableViewSection的封闭TableView上的数据是必要的:

I've fought with this issue myself and after a lot of trial and error I discovered that setting the data on the enclosing TableView of the TableViewSection is what's necessary:

var win = Ti.UI.currentWindow;
var tableview = Ti.UI.createTableView();
var sec1 = Titanium.UI.createTableViewSection();
var sec2 = Titanium.UI.createTableViewSection();
var data = [];

for(var v=0; v<=10; v++) {
    var row = Ti.UI.createTableViewRow({
        title:'Section 1 row '+v,
        className:'sectionrow'
    });
    sec1.add(row);
}
for(var c=0; c<=10; c++) {
    var row = Ti.UI.createTableViewRow({
        title:'Section 2 row '+c,
        className:'sectionrow'
    });
    sec2.add(row);
}

data[0] = sec1;
data[1] = sec2;
tableview.data = data;

win.add(tableview);

setTimeout(function() {
    alert('Adding additional rows to section 1');
    for(var x=0; x<=5; x++) {
        var row1 = Ti.UI.createTableViewRow({
            title:'Section 1 additional row '+x,
            className:'sectionrow'
        });
        sec1.add(row1);
    }
    alert('Adding additional rows to section 2');
    for(var y=0; y<=5; y++) {
        var row2 = Ti.UI.createTableViewRow({
            title:'Section 2 additional row '+y,
            className:'sectionrow'
        });
        sec2.add(row2);
    }
    // this is the line that makes the magic happen!
    tableview.data = data;
}, 3000);

祝你好运!