我是 Titanium Studio 的新手。我正在研究 TableView 。我在 tableviewrow 中添加了一些 ImageView 和标签。我可以将事件监听器添加到 TableView 行中的每个子级中吗?我正在循环内向表中添加行。下面是我的代码:
var tweetsTable = Ti.UI.createTableView({height:360, width: 306, top: 58, backgroundColor: '#FFFFFF',borderColor: '#C8C8C8',borderWidth:2, zIndex: -1});
var tweetsArray = [];
for(int i = 0; i < 5; i++)
{
var row = Ti.UI.createTableViewRow({contentHeight: 'auto', width: 320,top:0, selectionStyle : Titanium.UI.iPhone.TableViewCellSelectionStyle.NONE});
var my = Ti.UI.createView({ top:10,width:300,height:100 });
var accImage = Ti.UI.createImageView({Image: Ti.App.logoImage,width:50,height:50,left:10,top:5,borderRadius:4});
var addPlus = Ti.UI.createLabel({text:"+",color:"orange", font:{fontSize:18},borderWidth:0,bottom:5,right:20, width: 20, height: 20 });
var addMinus = Ti.UI.createLabel({text:"-",color:"orange", font:{fontSize:18},borderWidth:0,bottom:5,right:10, width: 20, height: 20 });
my.add(accImage);
my.add(addPlus);
my.add(addMinus);
row.add(my);
tweetsArray.push(row);
accImage.addEventListener('click', function(e){
alert(i);
}
addPlus.addEventListener('click', function(e){
alert(i);
}
addMinus.addEventListener('click', function(e){
alert(i);
}
}
我想为 accImage、addPlus、addMinus 添加事件监听器。但我无法分别找到 accImage、addPlus、addMinus 的点击事件。如果我在 for 循环内添加事件监听器,它可以工作,但最后一行子项只能工作。如果我单击第一行 addPlus,则最后一行 addPlus 正在工作。
如何为每行中的每个子项动态添加事件监听器。 任何人都可以吗..
最佳答案
我找到了解决方案。我的代码如下:
var tweetsTable = Ti.UI.createTableView({height:360, width: 306, top: 58, backgroundColor: '#FFFFFF',borderColor: '#C8C8C8',borderWidth:2, zIndex: -1});
var tweetsArray = [];
// response contains array of contents. The following loop will runs up to response length
for(int i = 0; i < response.length; i++)
{
var row = Ti.UI.createTableViewRow({contentHeight: 'auto', width: 320,top:0, selectionStyle : Titanium.UI.iPhone.TableViewCellSelectionStyle.NONE});
var my = Ti.UI.createView({ top:10,width:300,height:100 });
var accImage = Ti.UI.createImageView({Image: Ti.App.logoImage,width:50,height:50,left:10,top:5,borderRadius:4});
var addPlus = Ti.UI.createLabel({text:"+",color:"orange", font:{fontSize:18},borderWidth:0,bottom:5,right:20, width: 20, height: 20 });
var addMinus = Ti.UI.createLabel({text:"-",color:"orange", font:{fontSize:18},borderWidth:0,bottom:5,right:10, width: 20, height: 20 });
my.add(accImage);
my.add(addPlus);
my.add(addMinus);
row.add(my);
tweetsArray.push(row);
//find whether event from accImage
tweetsArray[i].children[0].addEventListener('click', function(e){
imageFunction(response[e.index]['name']);
});
//find whether event from addPlus
tweetsArray[i].children[1].addEventListener('click', function(e){
plusFunction(response[e.index]['name']);
});
//find whether event from addMinus
tweetsArray[i].children[2].addEventListener('click', function(e){
minusFunction(response[e.index]['name']);
});
}
我希望它对某些人有用。
关于iphone - 如何在 tableviewrow 中查找或跟踪子点击(Titanium Studio),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12776584/