2012-02-16 41 views
1

朋友你好,鈦手機:得到的tableview行值按鈕點擊問題

我正在開發使用谷歌廣場API中鈦開發一個應用程序,併成功地得到了在tableview中的數據,並顯示每一行,但中添加按鈕我的問題是,我想獲得名稱和地址按鈕點擊每行所以請給我想法或任何鏈接來解決我的問題。

請在我的屏幕截圖中查看關於btnclick的信息我想獲取姓名和地址。

在此先感謝。

var categoryName; 
var addressLabel; 
var row; 

var tableData=[]; 

PlacesListCells = function createRow() 
{ 
    //var tableData=[]; 

    var loader = Titanium.Network.createHTTPClient(); 

    var url = "https://maps.googleapis.com/maps/api/place/search/json?";  
    url = url + "location=" + lat + ',' + lon; 
    url = url + "&radius=" + radius; 
    url = url + "&name=" + name; 
    url = url + "&sensor=" + sensor; 
    url = url + "&key=" + key; 

    Ti.API.info(url); 
    // Sets the HTTP request method, and the URL to get data from 
    loader.open("GET",url); 
    // Create our HTTP Client and name it "loader" 
    // Runs the function when the data is ready for us to process 
    loader.onload = function() 
    { 
     var obj = JSON.parse(this.responseText); 
     Ti.API.log(obj);  
     var results = obj.results; 
     Ti.API.log(results); 


     for (var i = 0; i < results.length; i++) 
     { 
      categoryName = obj.results[i].name; 
      reference = obj.results[i].reference; 
      Ti.API.log('Refernce:'+reference); 

      // Create a row and set its height to auto 
      row = Titanium.UI.createTableViewRow({height:'78'}); 

      var placeImage = Titanium.UI.createImageView 
      ({ 
       image:'../iphone/appicon.png', 
       width:70, 
       height:50, 
       top:12, 
       left:5 
      }); 

      // Create the label to hold the tweet message 
      var nameLabel = Titanium.UI.createLabel({ 
       //text:name, 
       left:80, 
       top:5, 
       height:'auto', 
       width:185, 
       textAlign:'left', 
       font:{fontSize:12} 
      }); 

      // Create the label to hold the tweet message 
      addressLabel = Titanium.UI.createLabel({ 
       left:80, 
       top:25, 
       height:'auto', 
       width:185, 
       textAlign:'left', 
       font:{fontSize:14} 
      }); 

      var arrowImage = Ti.UI.createImageView 
      ({ 
       image:'../iphone/appicon.png', 
       width:20, 
       height:20, 
       left:280, 
       top:30 
      }); 

      var favoriteButton = Ti.UI.createButton 
      ({ 
       title:'btn', 
       //font:{fontFamily:'Helvetica Neue',fontSize:15}, 
       top:20, 
       left:255, 
       height:30, 
       width:50, 
       url:'../Images/favorite.png' 
       //image:'../Images/favorite.png' 
      }); 

      nameLabel.text = categoryName; 

      getDetailsData(addressLabel,row,i); 

      row.add(placeImage); 
      row.add(nameLabel); 
      row.add(addressLabel); 
      row.add(favoriteButton); 
      //row.add(arrowImage); 

      tableData[i] = row; 

      //set page title for each row 
      row.pageTitle = nameLabel.text; 

      favoriteButton.row = i; 
      favoriteButton.addEventListener('click', function(e) 
      { 
       Ti.API.log('favoriteButton clicked on row ' + e.source.row +' at ' + new Date().getSeconds()); 
       alert('favoriteButton clicked on row ' + e.source.row); 

       var index = e.source.row; 
       var name = tableData[index]; 
       alert('name'+name); 

      }); 
     }  
     tableView.setData(tableData);  
    }; 
    //-- Network error 
    loader.onerror = function(e) 
    { 
     Ti.API.info('Network error: ' + JSON.stringify(e)); 
    }; 

    // Send the HTTP request 
    loader.send(); 

    return tableData; 
}; 

enter image description here

+1

請發佈您嘗試的代碼或您正在使用的代碼。 – Tim 2012-02-16 05:46:31

+0

PLZ看到我更新的代碼,也成功索引按鈕上的按鈕,但無法獲得名稱和地址 – 2012-02-16 05:53:15

回答

1

一旦你有你的索引,你應該能夠通過引用您的原始數據源。

+0

我該怎麼做,PLZ給我代碼 – 2012-02-16 06:00:33

+0

你已經有了代碼 - 只要看看你** ** * * tableview行 – Tim 2012-02-16 06:00:55

+0

只是看到我更新的代碼和商店數據在tabledata數組,所以我怎麼能得到這個數組的名稱和地址 – 2012-02-16 06:12:02

1
 var favoriteButton = Ti.UI.createButton({ 
      title:'btn', 
      //font:{fontFamily:'Helvetica Neue',fontSize:15}, 
      top:20, 
      left:255, 
      height:30, 
      width:50, 
      url:'../Images/favorite.png' 
      //image:'../Images/favorite.png', 
      name:categoryName, 
      address:address;//address mention here 
     }); 

     row.add(favoriteButton); 

     favoriteButton.addEventListener('click', function(e) 
     { 
      var index = e.source.row; 
      var name = e.source.name; 
      var address = e.source.address; 
      alert('name'+name); 

     }); 
    } 

我認爲這可能會幫助你很多。