将sql行id传递给corona中的tableview

时间:2014-01-16 18:45:14

标签: sql lua tableview corona

这是我的代码,到目前为止工作正常

local count = 0
for row in db:nrows("select unit.id, builtin_list.* from unit inner join builtin_list on unit.id = builtin_list.unit_id group by artist order by artist asc") do
    count = count+1
    song[count]={}
    song[count].artist = row.artist
end

-- all done with db. close it
db:close()

local function tableViewListener (event)
    print(event.phase)
end

local function onRowRender(event) 
    local row = event.row
    local rowTitle = display.newText(row, song[row.index].artist, 10,0, nil, 14)
        rowTitle:setTextColor(0,0,0)
end

local function onRowTouch(event)

end

-- tableview
local tableView = widget.newTableView {
    left = 0, top = 0, 
    onRowRender = onRowRender, onRowTouch = onRowTouch, listener = scrollListener
}

for i = 1, count do
    local rowHeight = 40
    local rowColor = {
        default = {255,255,255}
    }
    local lineColor = { 220,220,220}

    tableView:insertRow {
        rowHeight = rowHeight,
        rowColor = rowColor,
        lineColor = lineColor,
        rowid = song[count].id
    }

end

这很好用。它显示来自sql的结果数据。我想通过将相同的sql表中的其他唯一属性传递到每一行来实现ontouch函数,这样当单击该行时,它将专门将该数据获取到该单击的行。我尝试将自定义变量添加到rowTitle(rowTitle.someData ='test'),但这似乎没有传递给onRowTouch事件。

2 个答案:

答案 0 :(得分:2)

我实际上通过添加params作为insertRow中的一个属性来修复此问题。然后我在里面叫它

我在sql循环中添加了另一个数组:

song[count].id = row.id

我添加了自定义参数:

tableView:insertRow({
    -- ...
    params = {paramID = song[i].id}
})

然后我将其称为触摸功能,如此

local function onRowTouch(event)
    print (event.target.params.paramID)
end

答案 1 :(得分:0)

你必须尝试一下。

function loadData()
local sql1 = "select unit.id, builtin_list.* from unit inner join builtin_list on unit.id = builtin_list.unit_id group by artist order by artist asc";
local projects = {}
for row in db:nrows(sql1) do
projects[#projects+1] =
{
    artist = row.artist;
}
end
return projects
end

本地数据= loadData()

在行访问数据中:

local row = event.row
local id = row.index
data[id].artist