我正在开发一个从 ACS(帖子对象)获取数据的应用程序。在我的应用程序中有按钮,当用户单击该按钮时,它会获取帖子数据。在每个帖子中还有一个photo_id,它允许我从云中获取照片网址。但是有一个问题,它有一个回调函数,当回调发生时为时已晚。
无论如何,下面的代码只显示最后一篇有照片的帖子,第一篇文章没有照片。(到目前为止只有 2 个帖子对象)。
我相信问题出在回调中,但我似乎无法解决它。
希望你们能帮到我,提前谢谢!
这是我的代码:
function getCarsClick() {
Cloud.Posts.query({
page: 1,
per_page: 20
}, function (e) {
if (e.success) {
var tableData = [];
for (var i = 0; i < e.posts.length; i++) {
var post = e.posts[i];
var row = Titanium.UI.createTableViewRow();
var title = Titanium.UI.createLabel({
text:post.title,
font:{fontSize:18,fontWeight:'bold'},
color:'#ef349d',
width:'auto',
textAlign:'left',
top:2,
left:40,
height:30
});
var subtitle = Titanium.UI.createLabel({
text:post.content,
font:{fontSize:12,fontWeight:'normal'},
color:'#000000',
width:'auto',
textAlign:'left',
bottom:10,
left:60,
height:32
});
row.add(title);
row.add(subtitle);
row.hasChild=true;
row.height = 80;
row.selectedBackgroundColor = '#ef349d';
Cloud.Photos.show({
photo_id: post.photo_id
}, function (e) {
if (e.success) {
var photo = e.photos[0];
var img1 = Ti.UI.createImageView({image:photo.urls.square_75,width:30,height:30,left:2, top:2});
row.add(img1);
} else {
alert('Error:n' +
((e.error && e.message) || JSON.stringify(e)));
}
});
tableData.push(row);
}
//add table data
$.tableview1.setData(tableData);
} else {
alert('Error:n' +
((e.error && e.message) || JSON.stringify(e)));
}
});
}
如果您使用提供的默认参数将照片传递到帖子中,则默认情况下图像应返回,并且不需要您进行额外的 API 调用来获取照片。
我还建议您,如果您刚刚开始使用Alloy,更重要的是使用ListViews而不是TableView
。https://github.com/aaronksaunders/AppC-Alloy-Book-Misc