2014-07-04 13 views
0

我正在開發一款帶有鈦/ appcelerator雲服務的PC的android應用程序。想要從Appcelerator雲中的數據指定每行tableView

我想要facebook的新聞源頁面,其中用戶的帖子列表下來。

我已經設法做到這一點與tableView與每行顯示從服務器的每篇文章。

問題是,我以某種方式無法獲得個人用戶的照片和(每個帖子有按鈕),如果我clikc這個按鈕,它不會給我那個特定用戶的用戶信息,但第一篇文章。

例如,如果user1,user2,user3分別發佈一個,則 列表頁面分別顯示post1,post2,post3。 如果我單擊post2(這是發送消息按鈕)的按鈕,它會嘗試將其發送到user1而不是user2。

這是我的代碼。

過去2天卡住了, 謝謝!

profileimage是照片中的變量 購物是消息變量發送按鈕

win.addEventListener('open', function() { 
Cloud.Posts.query({ 
    page: 1, 
    per_page: 20, 
    order: "-created_at" 
}, function (e) { 
    if (e.success) { 
     status.hide(); 

     var tbl_data = []; 
     for (var i = 0; i < e.posts.length; i++) { 
      var post = e.posts[i]; 

     -- there are some code here --- 

      var profileimage = Ti.UI.createView({ 
       backgroundImage: post.user.photo.urls.medium_500, 
       backgroundColor: '#fffff', 
       width: 90, 
       height: 90, 
       borderRadius: 22, 
       top:20, left: 10 
      }); 

      var shopping = Ti.UI.createView({ 
       backgroundImage: 'shopping.png', 
       width: 35, 
       height: 35, 
       top: 70, 
       right: 10 
      }); 

      shopping.addEventListener('click', function(){ 
       //alert(post); 
       var tradewin,tradeinc; 
        tradeinc = require('trade'); 
        tradewin = new tradeinc.trade1(post); 
        tradewin.open(); 
      }); 

回答

0

它好像您的問題從添加點擊事件監聽所有的shopping意見for循環內莖。當由於用戶操作實際調用shopping點擊功能時,它會訪問最新值post,該值在for循環的末尾將始終爲post的值:e.posts[e.post.length - 1]。我修改代碼來緩解這個問題:

win.addEventListener('open', function() { 
    Cloud.Posts.query({ 
     page: 1, 
     per_page: 20, 
     order: "-created_at" 
    }, function (e) { 
     if (e.success) { 
      status.hide(); 

      var tbl_data = []; 
      for (var i = 0; i < e.posts.length; i++) { 
       var post = e.posts[i]; 

      -- there are some code here --- 

       var profileimage = Ti.UI.createView({ 
        backgroundImage: post.user.photo.urls.medium_500, 
        backgroundColor: '#fffff', 
        width: 90, 
        height: 90, 
        borderRadius: 22, 
        top:20, left: 10 
       }); 

       var shopping = Ti.UI.createView({ 
        backgroundImage: 'shopping.png', 
        width: 35, 
        height: 35, 
        top: 70, 
        right: 10 
       }); 

       // This wrapper function returns a callback function 
       // for your click event that also maintains the correct 
       // value of `post` 
       function getShoppingClickCallback(associatedPost) { 
        return function() { 
         var tradewin, tradeinc; 

         tradeinc = require('trade'); 
         tradewin = new tradeinc.trade1(associatedPost); 
         tradewin.open(); 
        }; 
       } 

       // This line calls the `getShoppingClickCallback` function and 
       // passes the associated `post` object. The returned value of 
       // `getShoppingClickCallback` is a callback function. 
       shopping.addEventListener('click', getShoppingClickCallback(post)); 

使用輔助函數像getShoppingClickCallbackfor循環添加事件偵聽器時是一個真正有用的模式。我建議在您的項目中嘗試一下,看看您的問題是否仍然存在。我希望它不會! :)

+0

它工作!!!!!!!!!!!! thansk so much – user1927981

+0

不客氣!如果你不介意標記我的答案是正確的,將非常感謝! –

相關問題