使用Lua(Corona SDK)从表格中创建多个按钮。

我有一个表格,长这样:

table =
{
{
    id = 1,
    name = 'john',
    png = 'john.png',
    descr = "..."
},
{
    id = 2,
    name = 'sam',
    png = "sam.png",
    descr = "..."
}
...
}

有什么函数可以让我把表中每个图片都显示成如下按钮的样子 enter image description here 这样当我点击图片时就能打开相应的信息。

我卡在这里:

local buttons =  display.newGroup()
local xpos = -20
local ypos = 0
local e = -1

function addpicture ()
    for i=1, #table do
        xpos = (xpos + 100) % 300
        e = e + 1
        ypos = math.modf((e)*1/3) * 100 + 100
        local c = display.newImage( table[i].name, system.TemporaryDirectory, xpos, ypos)
        c:scale( 0.4, 0.4 )
        c.name = table[i].tvname
        buttons:insert(c)
    end
end

function buttons:touch( event )
    if event.phase == "began" then
        print(self, event.id)
    end
end
buttons:addEventListener('touch', buttons)
addpicture()

我如何认出被点击的图片以便回到相应的人的信息?

点赞
用户2690017
用户2690017

我通过像这样在循环内添加监听器来解决了我的问题:

function addpicture ()
    for i=1, #table do
       xpos = (xpos + 100) % 300
       e = e + 1
       ypos = math.modf((e)*1/3) * 100 + 100
       local c = display.newImage( table[i].name, system.TemporaryDirectory, xpos, ypos)
       c:scale( 0.4, 0.4 )
       c.name = table[i].tvname
       buttons:insert(c)
       function c:touch( event )
         if event.phase == "began" then
            print(self, event.id)
         end
       end
       c:addEventListener('touch', c)
    end
end
addpicture()
2015-02-28 09:12:29