我试图在Corona中设置一个表格视图,显示用户在文本字段对象中输入文本后创建的行。到目前为止,输入的文本应显示在创建的新行内。
我正在按照一个教程来做这个,它没有逐步解释如何做这些东西,并且tableviews远远超出我的编程能力,所以我要么错过了一些非常明显的东西,要么犯了一个结构错误...基本上我无法将输入的文本传递给要显示的行。在这里,有问题的片段:
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)
然后它在"提交" ' savename:userInput'我调用tableView:insertRow方法的函数,onRender称之为' makeRow'功能传递'事件'和' txt'如同参数。
' TXT'应该与用户输入的文本相对应,由本地txt = self.text'指定,但是当我运行程序时,会返回一个错误,说明字符串是预期的'在makeRow函数中创建行文本(row.t)时。
这似乎并没有正确传递该参数,但我无法弄清楚我犯了哪个错误。你们有什么想法???
感谢!!!
答案 0 :(得分:1)
我无法弄清楚可能会发生什么,但我会尝试做的是在函数savename中间打印self.text以查看其中的内容......
我认为可以解决您的问题的方法是用event.target.text ...
替换self.text希望它有所帮助!