iphone - 如何在 tableviewrow 中查找或跟踪子点击(Titanium Studio)

标签 iphone

我是 Titanium Studio 的新手。我正在研究 TableView 。我在 tableviewrow 中添加了一些 ImageView 和标签。我可以将事件监听器添加到 TableView 行中的每个子级中吗?我正在循环内向表中添加行。下面是我的代码:

var tweetsTable = Ti.UI.createTableView({height:360, width: 306, top: 58, backgroundColor: '#FFFFFF',borderColor: '#C8C8C8',borderWidth:2, zIndex: -1});
var tweetsArray = [];

for(int i = 0; i < 5; i++)
{

    var row = Ti.UI.createTableViewRow({contentHeight: 'auto', width: 320,top:0, selectionStyle : Titanium.UI.iPhone.TableViewCellSelectionStyle.NONE});

    var my = Ti.UI.createView({ top:10,width:300,height:100 });

    var accImage = Ti.UI.createImageView({Image: Ti.App.logoImage,width:50,height:50,left:10,top:5,borderRadius:4});

    var addPlus = Ti.UI.createLabel({text:"+",color:"orange", font:{fontSize:18},borderWidth:0,bottom:5,right:20, width: 20, height: 20 });

    var addMinus = Ti.UI.createLabel({text:"-",color:"orange", font:{fontSize:18},borderWidth:0,bottom:5,right:10, width: 20, height: 20 });

    my.add(accImage);
    my.add(addPlus);
    my.add(addMinus);

    row.add(my);
    tweetsArray.push(row);

    accImage.addEventListener('click', function(e){
        alert(i);
    }

    addPlus.addEventListener('click', function(e){
        alert(i);
    }

    addMinus.addEventListener('click', function(e){
        alert(i);
    }

}

我想为 accImage、addPlus、addMinus 添加事件监听器。但我无法分别找到 accImage、addPlus、addMinus 的点击事件。如果我在 for 循环内添加事件监听器,它可以工作,但最后一行子项只能工作。如果我单击第一行 addPlus,则最后一行 addPlus 正在工作。

如何为每行中的每个子项动态添加事件监听器。 任何人都可以吗..

最佳答案

我找到了解决方案。我的代码如下:

var tweetsTable = Ti.UI.createTableView({height:360, width: 306, top: 58, backgroundColor: '#FFFFFF',borderColor: '#C8C8C8',borderWidth:2, zIndex: -1});
var tweetsArray = [];

// response contains array of contents. The following loop will runs up to response length

for(int i = 0; i < response.length; i++)
{

    var row = Ti.UI.createTableViewRow({contentHeight: 'auto', width: 320,top:0, selectionStyle : Titanium.UI.iPhone.TableViewCellSelectionStyle.NONE});

    var my = Ti.UI.createView({ top:10,width:300,height:100 });

    var accImage = Ti.UI.createImageView({Image: Ti.App.logoImage,width:50,height:50,left:10,top:5,borderRadius:4});

    var addPlus = Ti.UI.createLabel({text:"+",color:"orange", font:{fontSize:18},borderWidth:0,bottom:5,right:20, width: 20, height: 20 });

    var addMinus = Ti.UI.createLabel({text:"-",color:"orange", font:{fontSize:18},borderWidth:0,bottom:5,right:10, width: 20, height: 20 });

    my.add(accImage);
    my.add(addPlus);
    my.add(addMinus);

    row.add(my);
    tweetsArray.push(row);

    //find whether event from accImage
    tweetsArray[i].children[0].addEventListener('click', function(e){
        imageFunction(response[e.index]['name']);
    });

    //find whether event from addPlus
    tweetsArray[i].children[1].addEventListener('click', function(e){
        plusFunction(response[e.index]['name']);
    });

    //find whether event from addMinus
    tweetsArray[i].children[2].addEventListener('click', function(e){
        minusFunction(response[e.index]['name']);
    });

}

我希望它对某些人有用。

关于iphone - 如何在 tableviewrow 中查找或跟踪子点击(Titanium Studio),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12776584/

相关文章:

ios - 当用户单击不同的单元格时,如何在一个 UiViewController 下显示多个 webview/URL

php - 如何在PHP中将Apple设备型号标识符转换为人类可读的文本?

iphone - 在不同的 iOS SDK 下编译一个 iphone 应用程序

iphone - URL 图片问题 iphone

iphone - 滑动手势不适用于ios 6中的表格 View

ios - 3x3 按钮的多点触控

iphone - 如何在 IOS6 中为 MPMoviePlayerController 仅提供一个景观 View

iphone - 当我进入编辑模式时,我可以阻止 UITableViewCell 缩小吗?

iphone - UIView 在转换 subview 时自动调整大小

iphone - 在我的应用程序中显示页面的实际 URL 时出现问题