gpt4 book ai didi

javascript - Titanium - 循环和 View 的内容问题

转载 作者:行者123 更新时间:2023-12-03 11:12:29 26 4
gpt4 key购买 nike


大家好,我是 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;


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

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

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


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

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

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

最佳答案

首先向行本身添加某种 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..
});
}

关于javascript - Titanium - 循环和 View 的内容问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27512138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com