corona sdk insert row tableview

struggling with a problem! I need to insert in a tableview the outcome of a DB query. The problem is that i cannot insert data since when i do it the result is nil...If i try to insert fixed data (like a general string "hello") it works! Can someone help me? Thanks a lot.

function listatornei()



    local dataURL = "http://www.lineitup.it/torneiopen.php?me="..usrUsr


    numtornei=0



local function listaListener(event)



if event.phase=="ended" then

  if (event.isError) then
  print("Connection not available")


  else

  id_tor = {} -- array to store query field1
  usr_tor = {} -- array to store query field2 


  local tab = event.response -- read  json data
  tab = json.decode( tab ) -- tabel conversion

    if #tab > 0 then

      for i=1, #tab do

        id_tor[i] = tab[i]["idt"]
        usr_tor[i] = tab[i]["gioc1_usr"] 

        numtornei=i
      end

    -- Listen for tableView events
      local function tableViewListener( event )
          local phase = event.phase

          print( event.phase )
      end

      -- Handle row rendering
      local function onRowRender( event )

          local phase = event.phase

          local row = event.row
          local id = row.index





           row.bg = display.newRect( 0, 0, display.contentWidth, 60 )
           row.bg.anchorX = 0
           row.bg.anchorY = 0
           row.bg:setFillColor( 1, 1, 0 )
           row:insert( row.bg )

           row.idt = display.newText(id_tor[i], 0, 0, native.systemFontBold, 40 )
           --row.idt = display.newText("pippo", 0, 0, native.systemFontBold, 40 )
           row.idt.anchorX = 0
           row.idt.anchorY = 0.5
           row.idt:setFillColor( 0 )
           row.idt.y = 20
           row.idt.x = 42

           row.usr = display.newText( usr_tor[i], 0, 0, native.systemFont, 40 )
           --row.usr = display.newText( "pluto", 0, 0, native.systemFont, 40 )
           row.usr.anchorX = 0
           row.usr.anchorY = 0.5
           row.usr:setFillColor( 0.5 )
           row.usr.y = 20
           row.usr.x = 250


           row:insert( row.idt )
           row:insert(  row.usr )


      end

      -- Handle row's becoming visible on screen
      local function onRowUpdate( event )

          local row = event.row

          print( "Row:", row.index, " is now visible" )
      end

      -- Handle touches on the row
      local function onRowTouch( event )
          local phase = event.phase

          if "press" == phase then
              print( "Touched row:", event.target.index )
          end
      end


      -- Create a tableView

    local tableView = widget.newTableView
    {
        top = 100,
        width = 620, 
        height = 410,
        --maskFile = "formlog1.png",
        listener = tableViewListener,
        onRowRender = onRowRender,
        onRowTouch = onRowTouch,
    }


  for i = 1, numtornei do

      local isCategory = false
      local rowHeight = 60
      local rowColor = 
      { 
          default = { 255, 255, 0 },
      }
      local lineColor = { 220, 220, 220 }

      -- Make some rows categories
      if i == 25 or i == 50  then
          isCategory = true
          rowHeight = 24
          rowColor = 
          { 
              default = { 150, 160, 180, 200 },
          }
      end

      -- Insert the row into the tableView
      tableView:insertRow
      {
          isCategory = isCategory,
          rowHeight = rowHeight,
          rowColor = rowColor,
          lineColor = lineColor,
      }
  end 


  end

  end

end
end

network.request( dataURL, "GET", listaListener )




end

You need to check your data coming back from the database. If the data from the database is nil, don't insert it. In your onRowRender function, you should test each bit of data to make sure it's not nil before you try to use to create an object.

In addition, you have a potential scope problem as well. onRowRender() is driven by an event of a table row becoming visible on the screen. It is not called synchronously inside of the for loop. The loop with your inserts will likely complete before the first row's onRowRender() completes. You also referencing the variable "i" inside onRowRender() which will be nil later when the rows render. If you database rows will always be in a 1 to 1 relationship with your tableView rows (ie no category rows), then you can use the row ID to look up your data in your data tables. If not, you should use the form of passing in data when you insert the rows. See: https://coronalabs.com/blog/2014/03/04/tutorial-advanced-tableview-tactics/ and look at the Passable Parameters section.

链接地址: http://www.djcxy.com/p/25900.html

上一篇: 如何在不使用AllowTransparency的情况下删除WPF窗口的非客户区

下一篇: 电晕sdk插入行tableview