这是我的代码,到目前为止工作正常
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 事件。
最佳答案
实际上,我通过添加 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
关于sql - 将sql行id传递给corona中的tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21170252/