1

我正在研究 Titanium 并为 iOS 设备开发。在我的应用程序中,我有一个tableview由函数创建的行填充的部分,该函数使用从HTTPClient对象响应接收到的数据。每行都有一个视图,在视图内有一个按钮和标签。

该按钮有一个click工作正常的事件。我向存储按钮的视图添加了一个单击事件,但是当我单击视图时,不会触发该事件,但是如果我单击该按钮,则会触发按钮的事件和视图的事件。这不是它应该表现的方式,因为我希望视图的事件做一些与按钮事件完全不同的事情。

为什么当我点击视图时视图的点击事件没有被触发?为什么单击按钮时会触发事件?

这是我创建行的方式:

function addToDo(title, priority){

    var that = {};      
    var row = Ti.UI.createTableViewRow({height:80});        
    that.currentPriority = priority;        
    that.resolveColor = function(tmpPriority){          
        switch(tmpPriority){                
            case 0: backgroundColorPriority = "#da362a"; break;
            case 1: backgroundColorPriority = "#da6c2a"; break;
            case 2: backgroundColorPriority = "#da962a"; break;
            case 3: backgroundColorPriority = "#dacb2a"; break;                         
        }               
        return backgroundColorPriority;
    }

    var rowLayout = Ti.UI.createView({
        backgroundColor : 'transparent'
    });

    var checkbox = Ti.UI.createButton({
        top: 25,
        left: 5,
        width: 30,
        height: 30,
        borderColor: 'white',
        borderWidth: 2,
        borderRadius: 1,
        backgroundColor: '#b1b1b1',
        backgroundImage: 'NONE',
        zIndex:10,
        value: false //value is a custom property in this case here.
    });
    rowLayout.add(checkbox);

    //Attach some simple on/off actions
    checkbox.on = function(item) {
        this.backgroundColor = '#62b425';

        item.currentRow.backgroundColor = "#101010";
        this.value = true;
    };

    checkbox.off = function(item) {
        this.backgroundColor = '#b1b1b1';
        item.currentRow.backgroundColor = item.resolveColor(item.currentPriority);
        this.value = false;
    };

    checkbox.addEventListener('click', function(e) {
        if(false == e.source.value) {
            e.source.on(that);
        } else {
            e.source.off(that); 
        }
    });

    // Create a Label.
    var todoTitleLabel = Ti.UI.createLabel({
        text : title,
        color : 'white',
        font : {fontSize:11},
        left : 40,
        textAlign : 'center'
    });

    // Add to the parent view.
    rowLayout.add(todoTitleLabel);

    row.add(rowLayout);

    rowLayout.addEventListener('click', function(e){
        // Whatever I put here isn't executed when I click on the rowLayout, instead I have to click the button to fire this event, that shouldn't happen
    });

    var backgroundColorPriority = that.resolveColor(that.currentPriority);      
    row.backgroundColor = backgroundColorPriority
    that.currentRow = row;
    return that.currentRow;

}

此函数在 HTTPClient onload 中调用:

var clientTask = Ti.Network.createHTTPClient({
    onload : function(e){
        var responseTask = JSON.parse(this.responseText);
        var entriesTask = responseTask.tasks;
        var todoSectionView = Ti.UI.createTableViewSection({
            headerTitle : responseTask.name
        });
        data.push(todoSectionView);
        for( var j=0; j < entriesTask.length; j++){
           var tmpRow = addToDo(entriesTask[j].name, entriesTask[j].priority);
           todoSectionView.add(tmpRow);
        }
        // add the data to the tableview
        table.data=data;
        self.updateLayout();
    },
    timeout : 60000
});
4

2 回答 2

0

我可能会通过为 todoTitleLabel 创建一个事件处理程序或给它一个与行不同的背景颜色来测试这一点,以查看我是否能够单击行本身。也许它上面的物体挡住了它?也许您可以通过将点击事件添加到标签来完成您想要做的事情?

于 2012-09-13T19:45:29.923 回答
0

不要将事件监听器添加到行、节或行布局中。它会在iOS上引起问题。

相反,将事件监听器添加到您的表视图中,例如:

tableView.addEventListener('click', function(e){
   var rowNumber = e.index;
   alert(rowNumber);
});

该示例将提醒所选行的编号。

于 2013-09-05T13:05:20.310 回答