select particular element in row of table view in titanium
hi I am developing Android application using Titanium. I want to change image on click event.But I am unable to select particular image in table view.I used following code:
var user_table = Ti.UI.createTableView({minRowHeight:5.length,hasChild:true});
var data = [];
for (var i=0;i<5.length;i++)
{
var row = Ti.UI.createTableViewRow({height:'auto',classNam开发者_开发百科e:"row"});
var username = Ti.UI.createLabel(
{
text:'user name',
height:'auto',
font:{fontSize:12, fontFamily:'Helvetica Neue', color:'#000'},
width:'auto',
color:'#000',
textAlign:'left',
top:0,
left:35,
});row.add(username);
var imageView = Ti.UI.createImageView(
{
image:'../images/user.png',
left:0,
top:0,
height:25,
width:25
});row.add(imageView);
}
feed_table.setData(data);
feedWin.add(feed_table);
I want to target image in particular row of table view so that i can replace it with another image on click event. help me for selecting that particular image
1) in your table view, set the event listener on the whole row.
tableView.addEventListener('click',function(event){
if ( event.source.id === undefined ) {
// if no id defined then you know it is not an image...
} else {
// you have an id, you have an image..
var rowNumber = event.index;
var image = event.source;
}
});
2) when you create your images, set an id on each one so you can identify it in the click event
var imageView = Ti.UI.createImageView({
id :"image_"+ i, // set object id
image:'../images/user.png',
left:0,
top:0,
height:25,
width:25
});
i would do it this way:
addRow = function(_args)
{
var row = Ti.UI.createTableViewRow(
{
height:'auto',
className:"row"
});
var username = Ti.UI.createLabel(
{
text: _args.text || 'user name',
height:'auto',
font:{fontSize:12, fontFamily:'Helvetica Neue', color:'#000'},
width:'auto',
color:'#000',
textAlign:'left',
top:0,
left:35,
});
row.add(username);
var imageView = Ti.UI.createImageView(
{
image:_args.image||'../images/user.png',
left:0,
top:0,
height:25,
width:25
});
row.add(imageView);
row.setImage = function(image)
{
imageView.imageURL = image;
};
row.addEventListener('click',function(e)
{
row.setImage('new/image/path');
};
return row;
}
var user_table = Ti.UI.createTableView({minRowHeight:5.length,hasChild:true});
var data = [];
for (var i=0;i<5.length;i++)
{
var newRow = addRow({
text: 'my text',
image: 'my image url'
});
data.push(newRow);
}
feed_table.setData(data);
feedWin.add(feed_table);
if you need to set the url you should also use
feedWin.data[0].rows[indexOfRow].setImage('another/image/path');
give it a try. didn't compile that, so it's just a proposal.
[update] be aware that 'auto' values may suck at android.
精彩评论