代码之家  ›  专栏  ›  技术社区  ›  Mike Trpcic

如何在钛合金的TableViewSection中附加行?

  •  13
  • Mike Trpcic  · 技术社区  · 14 年前

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

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

    这只是传递了一个参数,导致钛合金在意外情况下死亡:

    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 在“视图”和“行”中,我尝试了以下操作:

    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似乎不支持以下任何方法 appendRow insertRow ,所以我不知道还有什么好做的。我已经浏览过KitchenSink应用程序,但是没有任何例子可以让我在TableViewSection中添加一行。如有任何帮助,我们将不胜感激。

    3 回复  |  直到 11 年前
        1
  •  4
  •   rewinder    14 年前

    我自己也曾与此问题进行过斗争,经过多次尝试和错误之后,我发现在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);
    

    祝你好运!

        2
  •  1
  •   brian brinley    14 年前

    是否尝试在for add方法之外创建视图?看来这可能是构造函数的问题。

    尝试在for循环外创建一个通用视图,看看这是否能让您通过警告消息。

        3
  •  0
  •   Kelvin    11 年前

    复卷机说的很好。下面是类似的技巧,以一种更简单的形式呈现。在将新行添加到TableViewSection之后,请包含以下(看似愚蠢的)语句:

    $.table.data = $.table.data
    

    高温高压