【问题标题】:Accessing custom rows of a TableView访问 TableView 的自定义行
【发布时间】:2013-07-18 15:18:46
【问题描述】:

我将项目推送到数据数组,然后将它们添加到 rowData 数组,以便为​​表创建自定义行。当用户单击特定行时,我需要知道如何访问这些。当我有一个基本表时,我以前使用 e.rowData、title 等来访问这些元素,但现在它是一个自定义表,这不起作用。不用担心解析位,一切正常。感谢所有帮助!

data.push({

            title: items.item(i).getElementsByTagName("title").item(0).text,            
        leftImage: str.match(patt1) !== null ? str.match(patt1)[0] : 'image_news.png',
            dataToPass: items.item(i).getElementsByTagName("description").item(0).text,
            hasChild: true, 
            js:"external.js"
        });



    }

    var rowData=[];

    for(i=0;i<data.length;i++){
        var img= Titanium.UI.createImageView({
            image:data[i].leftImage,
            left:0,
            bottom:5,
            height: 100,
            width: 100
        });

        var bgBar=Titanium.UI.createView({
            height:110,
            width: "100%",
            bottom:0,
            left:0,
            backgroundColour: "#000",
            opacity: 0.6
        });

        var title=Titanium.UI.createLabel({
            text:data[i].title,
            color: 'black',
            left: 105
        });

        var row=Titanium.UI.createTableViewRow({
            height: "auto",
            hasChild: "true",
        js:"external.js",
        dataToPass: data[i].dataToPass
        });

        row.add(img);
        row.add(bgBar);
        row.add(title);



        rowData.push(row);
        console.log("row shiznick" + rowData);
    }


    tableView.setData(rowData);


tableView.addEventListener("click", function (e){
        console.log("HERE SOURCE.TITLE ------------------"+e.row.title);
        console.log("YOYOOYOYO------------"+e.source.title);
        console.log("IS THIS IT---------------"+e.children[0]);
        console.log("HERE IS THE SOURCE -----------------------------"+ e.source);
        console.log("HERE SOURCE.LEFTIMAGE REG EXP3 ------------------"+e.row.leftImage);
        console.log("HERE SOURCE.ClassName ------------------"+e.source.className);
        console.log("HERE HASCHILD ------------------"+e.rowData.hasChild);
        console.log("HERE DATATOPASS ------------------"+e.row.dataToPass);
});

【问题讨论】:

    标签: javascript arrays titanium element


    【解决方案1】:

    有两种方法可以设置表格的行,可以创建自定义行,也可以创建具有行对象属性的 JSON 对象。两种情况都有不同的方式访问行,row 对象或rowData 对象from the docs

    当使用 JavaScript 字典对象隐式创建行时,使用 [rowData 属性] 而不是 row 来访问任何自定义行属性。 这是一个隐式创建行的示例,这不是推荐的方式。

    在您的示例中,由于您没有隐式创建行对象,而是显式创建,您可以从事件的row 属性访问单击的行,如下所示:

    tableView.addEventListener("click", function(e){
        // Get the [TableViewRow](http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.TableViewRow) object
        var theRow = e.row;
        // Get the children
        var rowChildren = theRow.children;
        // Here are your objects in the row
        var imgInRow = rowChildren[0];
        var bgBarInRow = rowChildren[1];
        var titleInRow = rowChildren[2];
        // Here is the title
        var rowTitle = titleInRow.text;
    });
    

    或者,在这两种情况下,您始终可以使用索引来查找行对象,如下所示:

    tableView.addEventListener("click", function(e){
        // Get the index of the row
        var ndx = e.index;
        // Get the row, this only works if you have not added sections
        var row = tableView.data[ndx];
    });
    

    【讨论】:

    • 非常感谢您的回复。我唯一的问题是我不确定如何访问 dataToPass。我试过 var dataToPassInRow = rowChildren[3];和 dataToPassInRow.text 但这会导致错误吗?我认为这是因为我没有像添加其他属性那样添加此属性。有什么我想念的吗?
    • 也访问图像我试过 console.log(imgInRow.text) 返回未定义。 console.log(imgInRow.image) 但那是返回 [object imageView] 我试过 console.log(imgInRow.image.text) 是返回 undefined
    • hmm e.rowData 似乎适用于 dataToPass。这令人困惑。我仍然无法获取图像
    • 像 dataToPass 一样将图像附加到行,然后像 e.rowData.image 那样获取它
    【解决方案2】:

    为了在用户点击时访问行的数据(即使是在自定义行的情况下不显示的数据),您可以创建一个数组,在循环期间将数据推送到其中并使用 e 访问它。指数。 一个简单的代码示例:

        var allDataArray = [];
    
            // loop through each item
            for ( var i = 0; i < items.length; i++ )
            {
                // Your source datas (custom row : id is not displayed in the rows)
                var sourceData = {
                    id: items.item(i).getAttribute('id'),               
                    title: items.item(i).getElementsByTagName("title").item(0).textContent,
                };
    
                // Saving data for each loop
                allDataArray.push(sourceData);
    }
    

    然后访问数据

    itemsTable.addEventListener('click', function(e)
            {
                // Target the array with e.index offset
                var rowContent = allDataArray[e.index];
                Ti.API.info(rowContent); // will display every single data of the current row clicked
                alert(rowContent.id) // will display the "hidden" id which was not displayed in the UI
    
    }
    

    【讨论】:

      【解决方案3】:

      尝试使用 event.tget 函数,例如:

      $(".row").click(function(event) {
        var element = event.target;
        // Accessing element
      });
      

      【讨论】:

      • 不,它是 javascript,正在访问一个 dom 属性。
      • 这导致应用程序崩溃。我没有收到任何特定的错误消息
      • 你可以制作一个 jsFiddle 来观察正在发生的事情。
      • OP 要求的是 Titanium,而不是 jQuery,Titanium 中没有 DOM,这将不起作用。
      • @Hernandcb 我不能为钛项目做 jsfiddle
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      相关资源
      最近更新 更多