访问的TableView自定义行 [英] Accessing custom rows of a TableView

查看:204
本文介绍了访问的TableView自定义行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我推项的数据阵列,然后以一个表中创建自定义的行添加这些给rowData阵列。我需要知道如何当用户点击一个特定的行访问它们。我是用e.rowData previously,用于访问这些元素,当我有一个基本的表格标题等,但现在,它是一个自定义表,这是行不通的。不要担心解析位,这一切工作正常。所有帮助AP preciated!

  data.push({            标题:items.item(I).getElementsByTagName(称号)项目(0)的.text,
        leftImage:str.match(patt1)== NULL!? str.match(patt1)[0]:'image_news.png',
            dataToPass:items.item(I).getElementsByTagName(说明)项目(0)的.text,
            hasChild:真实,
            JS:external.js
        });    }    变种rowData = [];    对于(i = 0; I< data.length;我++){
        VAR IMG = Titanium.UI.createImageView({
            影像:数据[I] .leftImage,
            左:0,
            底部:5,
            高度:100,
            宽度:100
        });        VAR bgBar = Titanium.UI.createView({
            身高:110
            宽度:100%,
            底部:0,
            左:0,
            backgroundColour:#000
            不透明度:0.6
        });        VAR标题= Titanium.UI.createLabel({
            文本:数据[I] .title伪,
            颜色:黑,
            左:105
        });        VAR行= Titanium.UI.createTableViewRow({
            高度:自动,
            hasChild:真,
        JS:external.js
        dataToPass:数据[I] .dataToPass
        });        row.add(IMG);
        row.add(bgBar);
        row.add(职称);        rowData.push(行);
        的console.log(排shiznick+ rowData);
    }
    tableView.setData(rowData);
tableView.addEventListener(点击,功能(E){
        的console.log(这里SOURCE.TITLE ------------------+ e.row.title);
        的console.log(YOYOOYOYO ------------+ e.source.title);
        的console.log(这是IT ---------------+ e.children [0]);
        的console.log(这是源-----------------------------+ e.source);
        的console.log(在这里SOURCE.LEFTIMAGE REG EXP3 ------------------+ e.row.leftImage);
        的console.log(这里SOURCE.ClassName ------------------+ e.source.className);
        的console.log(这里HASCHILD ------------------+ e.rowData.hasChild);
        的console.log(这里DATATOPASS ------------------+ e.row.dataToPass);
});


解决方案

有两种方式设置表格的行,您可以创建自定义行,或者创建拥有行对象的属性JSON对象。这两种情况在访问行不同的方式,无论是对象或 rowData 对象的从文档


  

在创建行实际上使用JavaScript字典对象,请使用[该rowData属性],而不是一行访问任何自定义行属性。
  下面是隐式创建一排,这是不推荐的方式的一个例子。


在你的榜样,因为你没有隐含地创建行对象,但明确的,你可以从一行访问点击行这样的事件的属性:

  tableView.addEventListener(点击,功能(E){
    //获取[TableViewRow](http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.TableViewRow)对象
    VAR theRow = e.row;
    //把孩子们
    VAR rowChildren = theRow.children;
    //这里是你的对象的行
    变种imgInRow = rowChildren [0];
    变种bgBarInRow = rowChildren [1];
    变种titleInRow = rowChildren [2];
    //这里是标题
    VAR rowTitle = titleInRow.text;
});

另外,在这两种情况下,你总是可以使用索引来查找行对象是这样的:

  tableView.addEventListener(点击,功能(E){
    //获取行索引
    VAR NDX = e.index;
    //获取行,这仅适用于如果你还没有加入部分
    VAR行= tableView.data [NDX]
});

I am pushing items to a data array and then adding these to a rowData array in order to create custom rows for a table. I need to know how to access these when a user clicks a specific row. I was previously using e.rowData,title etc. for accessing these elements when I had a basic table but now that it is a custom table, this isn't working. Don't worry about the parsing bit, that all works fine. All help appreciated!

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

解决方案

There are two ways to set the rows of a table, you can create custom rows, or create JSON objects that have the attributes of the row object. Both cases have different ways of accessing the row, either the row object or the rowData object, from the docs:

When the row is created implicitly using a JavaScript dictionary object, use [the rowData property] rather than row to access any custom row properties. Here's an example of creating a row implicitly, which is not the recommended way.

In your example, since you did not create the row objects implicitly, but explicitly, you can access the clicked row from the row attribute of the event like this:

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

Alternatively, in both cases, you could always use the index to look up the row object like this:

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

这篇关于访问的TableView自定义行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆