我正在构建一个 Titanium iOS 应用程序。我有一个表格 View ,每行都有两个图像和一些其他标签。我希望能够单击每一张图像并交换其他图像,并且我希望能够单击电话号码等标签并调出拨号器。到目前为止,单击行中的任意位置都会交换图像。这是我的一些代码:
//First I create my table and then call a php file to return some JSON data, then I create my row and define an image
row = Ti.UI.createTableViewRow({
backgroundImage : 'images/openmatchesrowbackground.png',
selectionStyle : Titanium.UI.iPhone.TableViewCellSelectionStyle.NONE,
width : '100%',
height : '180pt',
rowId : i
});
var acceptmatchView = Ti.UI.createView({
left : '0pt',
top : '0pt',
width : '60pt',
height : '60pt'
});
var acceptmatch = Ti.UI.createImageView({
image : 'images/nomatch.png',
left : '0pt',
top : '0pt',
width : '60pt',
action: 'swapImages',
height : '60pt'
});
//Then I add some labels and add everything to the row - Now create my event listener
tableview.addEventListener('click', function(e) {
var imageView = e.row.children[0].children[0];
if (imageView.image == 'images/nomatch.png') {
imageView.image = 'images/match.png';
var matchSelected = json.openmatches[e.rowData.rowId];
var alertWindow = Titanium.UI.createAlertDialog({
title : 'Accept This Match?',
message : 'Are you sure you want to accept this match?' + '\n' + matchSelected.matchtype + '\n' + 'Time: ' + matchSelected.datetime + '\n' + 'At: ' + matchSelected.courtname,
cancel : 1,
buttonNames : ['Yes', 'Cancel']
});
alertWindow.addEventListener('click', function(ev) {
Titanium.API.info("cancel " + ev.cancel);
Titanium.API.info("index " + ev.index);
switch(e.source.action){
//switch(ev.index) {
//case 0:
case 'swapImages':
break;
case 'swapImages':
imageView.image = 'images/nomatch.png';
break;
}
});
alertWindow.show();
} else {
imageView.image = 'images/nomatch.png';
var alertWindow = Titanium.UI.createAlertDialog({
title : 'Cancel This Match?',
message : 'Are you sure you want to cancel this match?',// + '\n' + matchSelected.matchtype + '\n' + 'Time: ' + matchSelected.datetime + '\n' + 'At: ' + matchSelected.courtname,
cancel : 1,
buttonNames : ['Yes', 'Keep Match']
});
alertWindow.addEventListener('click', function(ev) {
Titanium.API.info("cancel " + ev.cancel);
Titanium.API.info("index " + ev.index);
switch(e.source.action) {
case 'swapImages':
break;
case 'swapImages':
imageView.image = 'images/match.png';
break;
}
});
alertWindow.show();
}
});
tableview.setData(tableData);
},
如何编写代码以便它可以在行中的每个对象上运行?
最佳答案
除了 Dawson Toth 的回答外,请尝试重置 bubbleParent属性为假。在您的情况下,事件可能会传播到父级。在这里,您已将事件监听器添加到 tableViewRow。因此,对子级的每次点击都会传播到父级。 bubbleParent
的默认值为 true。将 bubbleParent
设置为 false
将不会传播该操作。
var acceptmatchView = Ti.UI.createView({
left : '0pt',
top : '0pt',
width : '60pt',
height : '60pt',
bubbleParent: false
});
var acceptmatch = Ti.UI.createImageView({
image : 'images/nomatch.png',
left : '0pt',
top : '0pt',
width : '60pt',
action: 'swapImages',
height : '60pt',
bubbleParent: false
});
关于javascript - 连续多个对象的事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23661497/