将SQL行ID传递给Corona中的TableView

这是我的代码,目前运行良好

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

-- 完成了对数据库的所有操作。关闭它
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 事件里。

点赞
用户982288
用户982288

我通过在insertRow中添加params作为属性来修复了这个问题。然后在循环中添加了另一个数组:

song[count].id = row.id

我添加了自定义参数,如下所示:

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

然后在触摸函数中调用它,如下所示:

local function onRowTouch(event)
    print (event.target.params.paramID)
end
2014-01-16 19:16:45
用户4294620
用户4294620

你必须尝试一下。

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

在行内访问数据:

local row = event.row
local id = row.index
data[id].artist
2015-02-03 12:15:05