Question

I am building a Titanium iOS app. I am receiving JSON data from a server and populating a table. I have an image in each row that I swap out when the image is selected, then I show an alert to confirm their selection. My problem is displaying the SELECTED rows data in the alert. Here is my code:

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

How do I get the selected rows data to show in the alert?

Was it helpful?

Solution

For that, you need to change the code like following

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

I'm using a custom property rowId for identifying the selected row.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top