javascript - 如何将一行附加到 Titanium 中的 TableViewSection?

标签 javascript iphone titanium

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

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

这只是传递一个参数,这会导致 Titanium 死掉并出现未捕获的异常:

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 ”和“行”,因此我尝试了以下操作:

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

上面的代码没有抛出异常,但是它给出了一个[WARN]:

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

TableViewSections 似乎不支持任何方法,例如 appendRowinsertRow,所以我不知道还能用它做什么。我查看了 KitchenSink 应用程序,但没有找到向 TableViewSection 添加行的示例。感谢您的帮助。

最佳答案

我自己也曾与这个问题作过斗争,经过大量试验和错误后,我发现在 TableViewSection 的封闭 TableView 上设置数据是必要的:

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);

祝你好运!

关于javascript - 如何将一行附加到 Titanium 中的 TableViewSection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2721705/

相关文章:

ios - 适用于 iPhone 的最佳分析产品

iOS 应用拒绝 IPv6(没有任何 http 请求)

javascript - Prototype Class.create 没有正确地子类化

iphone - 是否可以在 GameKit 中制作运行的高分排行榜?

iphone - for(NSString *key in [rewards allKeys]) 使应用程序崩溃?

android - 我如何在我的钛项目中使用本地镜像

javascript - Titanium iPhone Mapview 不显示来自引脚注释的标题或副标题

javascript - 使用 MVC、C# 和 AngularJS 时如何在页面加载时保留页面的下拉值?

javascript - 如何检测默认下拉选项与使用 js/jquery 选择的选项?

JavaScript:使用 window.setTimeout 触发 CSS Transition