Tableview in corona sdk

I'm trying to set up a table view in Corona that displays rows which are created once the user inputs a text in a textfield object. So far the text inputed should be shown inside of the new row created.

I am following a tutorial to do this which doesn't explain step by step how to do this stuff and tableviews are well above my programming capabilities so I'm either missing something very obvious or making a structural mistake... Basically I am not able to pass the text inputed to the row to be displayed. Here,s the snippet in question:

local savename = native.newTextField( 0, 0, display.contentWidth - 50, 24 )
      sceneGroup:insert (savename)

      savename.x = centerX
      savename.y = display.screenOriginY + 140   
      savename.inputType = "default"
      savename:setTextColor( 0, 0, 0 )
      savename.align = "center"

local function scrollListener (event)
end

local function makeRow (event, txt)
  local row = event.target;

  row.t = display.newText(txt,0,0,"Helvetica",18)
  row.t.anchorX = 0
  row.t.y = row.height * 0.5
end

local function handleRowTouch (event)
end

local tableView = widget.newTableView( {
    top = display.contentHeight/2 + 90,
    width = display.contentWidth,
    height = 240,
    listener = scrollListener,
    onRowRender = makeRow,
    onRowTouch = onRowTouch
} )

sceneGroup:insert (tableView)

      local savename = native.newTextField( 0, 0, display.contentWidth - 50, 24 )
      sceneGroup:insert (savename)

      savename.x = centerX
      savename.y = display.screenOriginY + 140   
      savename.inputType = "default"
      savename:setTextColor( 0, 0, 0 )
      savename.align = "center"

      function savename:userInput (event)

        local length = string.len( self.text )

        if (event.phase == "began") then
          print ("Began" .. event.target.text)
          elseif (event.phase == "editing") then
                      print ("editing" .. event.target.text)

            elseif (event.phase == "ended") then
                      print ("ended" .. event.target.text)

            elseif (event.phase == "submitted") then

                    if (length > 0)  then

                      local rowHeight = 38
                      local rowColor = {default = { 1, 1, 1}}
                      local lineColor = {0, 0, 0}

                      local txt = self.text      

                      tableView:insertRow({

                        onEvent = handleRowTouch,
                        onRender = function (event) makeRow (event, txt) end,

                        height = rowHeight,
                        rowColor = rowColor,
                        lineColor = lineColor                      
                        })

                      self.text = ""
                      setKeyboardFocus (nil)

                    else

                      local alert = native.showAlert("Hey!", "Type in a save name please...n Or dismiss this action by touching the background.", {"Ok"})

                    end

        end
      end  

      savename:addEventListener ("userInput", savename)

Then it's in the "submitted" phase of the 'savename:userInput' function that I'm calling the tableView:insertRow method which onRender calls the 'makeRow' function passing 'event' and 'txt' as params.

'txt' should correspond to the text input by the user as specified by 'local txt = self.text', but when I run the program an error is returned saying 'a string is expected' when creating the row text (row.t) in the makeRow function.

This seems as I'm not passing that parameter correctly but I can't figure out where I'm making a mistake. Do you guys have any idea why???

Thanks!!!


I can't figure out what might be happening, but what I would try doing is printing self.text in the middle of function savename to see what's in it...

What I think that might solve your problem is replacing self.text with event.target.text ...

Hope it helps!

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

上一篇: 电晕sdk插入行tableview

下一篇: 在电晕sdk的Tableview