【问题标题】:Titanium - Content Issue with Loops and ViewsTitanium - 循环和视图的内容问题
【发布时间】:2014-12-16 19:12:10
【问题描述】:


嗨,我是 Titanium 的新手,在我的项目中,我正在循环访问一些为我的应用程序页面标题和内容提供服务的 JSON 数据。我有这个功能:

xhr.onload = function() {
    try {
    var myPages = JSON.parse(this.responseText);

    for (var c=0;c<myPages.length;c++){ 
      var title = myPages[c].title; // page title
      var content = myPages[c].content; // page content


我已将页面标题作为标签添加到 TableViewRow:

      var row = Ti.UI.createTableViewRow({hasChild:true,height:Ti.UI.SIZE,backgroundColor:bgcolor});

      // Create a vertical layout view to hold all the titles
      var post_view = Ti.UI.createView({
          height: Ti.UI.SIZE,
          layout:'vertical',
          left:5,
          etc..
      });          

      // Create article titles
      var label_title = Ti.UI.createLabel({
          text:title,
          left:5,
          etc...
      });

    // Add the article titles to the view
    post_view.add(label_title);

    // Add the vertical layout view to the row
    row.add(post_view);
    row.className = 'item'+c;
    data[c] = row;


这一切都很好,我得到一个表格,每行都有我的页面标题,但是当用户单击标题时,我希望应用程序打开一个新视图/窗口以显示相应页面的内容。这就是我的麻烦开始的地方!

我添加了这个函数来尝试处理这种情况:

    // Add an event listener to the rows
    row.addEventListener('click', function(){
        Titanium.API.info('row has been clicked');

        // Create view for article content
        var articleView = Titanium.UI.createView({
            height: Ti.UI.SIZE,
            layout:'vertical',
            left:5,
            etc..
        });

        var t = Ti.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT;
        win.animate({view:articleView,transition:t});

        var articleViewContent = Ti.UI.createLabel({
            text:content,
            left:5,
            etc..
        });

        // Add the content to the view
        articleView.add(articleViewContent);

     });
     }

    // Create the tableView and add it to the window.
    var tableview = Titanium.UI.createTableView({data:data,minRowHeight:58});
    win.add(tableview);

    }
    catch(E){
        alert(E);
    }
};


我可以得到对标题点击输入的响应以显示在控制台中,但我不知道如何在新视图中调用相应的内容。我认为计数器应该存储这个(从创建标题时开始),但不知道如何访问它。

我有点迷茫,觉得我可能在这里搞砸了一些基础知识。我对 JavaScript 也比较陌生,所以请原谅任何错误!如果能得到一些建议来帮助我改进,那就太好了!

(我在代码中添加了一些 'etc..' 以缩短内容)

【问题讨论】:

    标签: javascript json titanium titanium-mobile


    【解决方案1】:

    首先将某种content 属性添加到行本身,以便稍后通过行单击事件访问它。如下调整行定义:

    var row = Ti.UI.createTableViewRow({
        content: content,  // contains the article content from myPages[c].content
        hasChild:true,
        height:Ti.UI.SIZE,
        backgroundColor:bgcolor
    });
    

    然后你需要将事件传递给你的事件监听器中的回调函数:

    row.addEventListener('click', function(ev){
    
        Titanium.API.info('row has been clicked:'+JSON.stringify(ev)); // this will allow you to see the event's properties
    
        ...
    
        // you can access the content attribute of the row by using ev.row.content
        var articleViewContent = Ti.UI.createLabel({
            text:ev.row.content,
            left:5,
            etc..
        });
    }
    

    【讨论】:

    • 这很好,感谢您的帮助 Ed。我很高兴我并没有太远!我也不是 100% 确定是否应该在循环中包含 row.addEventListener 函数,但这一切似乎都运行良好。
    • @niallobr - 没问题,很乐意提供帮助。请注意,事件对象在 Android 上的结构可能略有不同——我从来没有找到任何好的文档来说明所有各种事件对象的结构。我总是不得不用Ti.API.info 来检查它,才能知道如何从中获取我需要的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    相关资源
    最近更新 更多