问题描述
我是Titanium Studio的新手.我正在工作表视图.我在tableviewrow中添加了一些Image视图和Labels.我可以向表视图行中的每个子级添加eventlistener吗?我在循环内向表中添加行.下面是我的代码:
I am new to Titanium Studio. I'm working on table view. I've added some Image views and Labels in a tableviewrow. Can I add eventlistener to the each children in table view row. And I'm adding rows to the table inside a loop. Below is my code:
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的点击事件.如果我在循环中添加事件侦听器,则它可以正常工作,但最后一行只能工作.如果我单击第一行addPlus,则最后一行addPlus正常工作.
I want to add event listener for accImage,addPlus,addMinus. But i unable to find click event of accImage,addPlus,addMinus separately. If i add event listener inside for loop it working, but the last row children only working. If i click first row addPlus, then the last row addPlus is working.
如何为每行中的每个子级动态添加事件侦听器.可以请任何一个人..
How can i add event listener dynamically for each children in each row.Can any one please..
推荐答案
我找到了解决方案.我的代码如下:
I found the solution. My code is below:
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']);
});
}
我希望它对某些人有用.
I hope it will useful to some one.
这篇关于如何在tableviewrow中查找或跟踪子点击(Titanium Studio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!