Search code examples
androidluawidgettableviewcoronasdk

How can I access, to the row content in a TableView, by clicking the row


everyone: This is my problem, I'm trying to show the content of a selected row in a TableView, when I clicked.

In the onRowRender I add the rows by this way,

local function onRowRender( event )    
  local row = event.row 
  local myText = ""    
  local myText = display.newText(row,"Text to show",150,row.contentHeight/2,native.systemFontBold,30 )
end

In the onRowTouch event I have tried this,

local function onRowTouchTable( event )    
  print(menuPedidos._view._rows[row.index]) //it return a table, I assumed the row selected
  print(menuPedidos._view._rows[row.index].mytext) //it return nil
end 

And what I need, is the text saved in the object mytext

I will appreciate any help, thank you


Solution

  • I think you should store your data in separate variable, pass it in params in insertRow and set row's id also in insertRow to reference to them later.

    Example

    for i = 1, #myData do
       myList:insertRow{
          rowHeight = 60,
          id = i,
          isCategory = false,
          rowColor = { 1, 1, 1 },
          lineColor = { 0.90, 0.90, 0.90 },
          params = {
             name = myData[i].name,
             phone = myData[i].phone
          }
       }
    end
    

    So in onRowTouchTable you can do something like that

    local function onRowTouchTable(event) 
       local row = event.target
       local id = row.index -- or row.id I'm not sure
    
       print(myData[id])
    end
    

    More information you find on Corona blog. Also, you can see youtube video Corona University - Displaying Database Data Using TableView Widgets.