【问题标题】:Expandable Table View Row - like Tree Error why?可扩展表视图行 - 为什么像树错误?
【发布时间】:2014-03-13 10:04:48
【问题描述】:

错误 在应用中找不到索引行

我正在处理这个代码

  1. 如果我单击第一次时间表视图行,它将显示错误

    但它会显示子行。

    2.如果我单击子行,则会显示此错误:未定义不是对象

    (评估(e.row.sub.length)

    为什么会出现这个错误?

    代码

    var win = Ti.UI.createWindow();
    var container = Ti.UI.createView({ backgroundColor: "white", layout: "vertical" });
    
    var layout = [{
            title: "Parent 1",
            isparent: true,
            opened: false,
            sub: [
                { title: "Child 1" },
                { title: "Child 2" }
            ]
        }, {
            title: "Parent 2",
            isparent: true,
            opened: false,
            sub: [
                { title: "Child 3" },
                { title: "Child 4" }
            ]
        }];
    
    var tableView = Ti.UI.createTableView({
        style: Titanium.UI.iPhone.TableViewStyle.GROUPED,
        top: 0,
        height: Ti.Platform.displayCaps.platformHeight,
        data: layout
    });
    
    
    tableView.addEventListener("click", function (e) {
        var i;
        //Is this a parent cell?
        console.log(e.row);
        if (e.row.isparent) {
            //Is it opened?
            if (e.row.opened) {
                for (i = e.row.sub.length; i > 0; i = i - 1) {
                    tableView.deleteRow(e.index + i);
                }
                e.row.opened = false;
            } else {
                //Add teh children.
                var currentIndex = e.index;
                for (i = 0; i < e.row.sub.length; i++) {
                    tableView.insertRowAfter(currentIndex, e.row.sub[i]);
                    currentIndex++;
                }
                e.row.opened = true;
            }
        }
    });
    
    container.add(tableView);
    
    win.add(container);
    win.open();
    

    任何帮助将不胜感激

【问题讨论】:

    标签: ios titanium titanium-mobile


    【解决方案1】:

    您的代码的问题是,当表索引尚未更新时,您试图在表的末尾插入许多行。它的解决方案是使用相同的索引以相反的顺序添加行。

    这是您的事件监听器的修改版本:

    tableView.addEventListener("click", function (e) {
        var i, rows;
        //Is this a parent cell?
        if (e.row.isparent) {
            //Is it opened?
            if (e.row.opened) {
                for (i = e.row.sub.length; i > 0; i = i - 1) {
                    tableView.deleteRow(e.index + i);
                }
                e.row.opened = false;
            } else {
                //Add teh children.
                rows = e.row.sub.reverse();
                for (i = 0; i < rows.length; i++) {
                    tableView.insertRowAfter(e.index, rows[i]);
                }
                e.row.opened = true;
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多