我正在构建一个 Titanium iOS 应用程序。我正在从服务器接收 JSON 数据并填充表。我在每行都有一个图像,当选择该图像时我会交换该图像,然后我会显示一个警报以确认他们的选择。我的问题是在警报中显示选定的行数据。这是我的代码:
//Create the row
var xhr = Ti.Network.createHTTPClient({
onload : function() {
Ti.API.debug(this.responseText);
var json = JSON.parse(this.responseText);
for ( i = 0; i < json.matches.length; i++) {
match = json.matches[i];
row = Ti.UI.createTableViewRow({
backgroundImage : 'images/matchesrowbackground.png',
selectionStyle : Titanium.UI.iPhone.TableViewCellSelectionStyle.NONE,
width : '90%',
height : '180px'
});
var acceptmatchView = Ti.UI.createView({
left : '0px',
top : '0px',
width : '60px',
height : '60px'
});
var acceptmatch = Ti.UI.createImageView({
image : 'images/nomatch.png',
left : '0px',
top : '0px',
width : '60px',
height : '60px'
});
//Add some more stuff to the row and add the rows to the table then do my tableview.EventListener
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 alertWindow = Titanium.UI.createAlertDialog({
title : 'Accept This Match?',
message : 'Are you sure you want to accept this match? ' + '\n' + match.matchtype + '\n' + 'Time: ' + match.datetime + '\n' + 'At: ' + match.cname,
cancel : 1,
buttonNames : ['Yes', 'Cancel']
});
如何获取要在警报中显示的选定行数据?
最佳答案
为此,您需要像下面这样更改代码
//Create the row
var xhr = Ti.Network.createHTTPClient({
onload : function() {
Ti.API.debug(this.responseText);
var json = JSON.parse(this.responseText);
for ( i = 0; i < json.matches.length; i++) {
match = json.matches[i];
row = Ti.UI.createTableViewRow({
backgroundImage : 'images/matchesrowbackground.png',
selectionStyle : Titanium.UI.iPhone.TableViewCellSelectionStyle.NONE,
width : '90%',
height : '180px',
rowId : i //custom property
});
//Add some more stuff to the row and add the rows to the table
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.matches[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.cname,
cancel : 1,
buttonNames : ['Yes', 'Cancel']
});
}
});
我正在使用自定义属性 rowId
来标识所选行。
关于javascript - 从警报中的选定行检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23545008/