2014-01-16 39 views
2

這是我的代碼和工作的罰款,到目前爲止傳遞SQL行ID來實現代碼如下電暈

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

我實際上通過添加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 
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