钛 - 循环和视图的内容问题
Titanium - Content Issue with Loops and Views
嗨,我是 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 也比较陌生,所以请原谅任何错误!如果能得到一些建议来帮助我改进,那就太好了!
(我在代码中添加了一些"等"以缩短内容(
首先向行本身添加某种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..
});
}
相关文章:
- 骨干视图问题
- 很多主干视图-性能问题
- kenodui树视图滚动到节点问题
- 在ajax分页视图(cakephp)中显示adsense的问题
- 将JSON REST请求加载到Kendo ui列表视图中的问题
- Ionic和AngularJS中遍历视图的问题
- 将部分视图加载到JQuery对话框中的缓存问题
- AngularJS-$routeProvider和数据ng视图的问题
- Play Framework 2.0-渲染视图中的问题
- 使用ng repeat添加多个视图实例时的范围界定问题
- 单击视图时的应用器范围问题
- DHTMLX 移动调度程序事件详细信息视图高度问题
- 加载视图以查看问题 - AngularJS + ui.router
- ASP.net VB 详细信息视图插入:取消问题的 javascript 回发
- MVC4 部分视图 JavaScript 捆绑问题
- 自定义元素问题:视图模型中的ref元素为null
- Aptana 3.0.5问题视图与JavaScript
- Angular $scope和ng-if/ng-show问题.视图中的状态没有改变
- 构造JavaScript的问题(视图和模型逻辑分离)
- Gmaps Api V3在Facebox问题视图