I have been having a lot of trouble getting my tableview to work, Below is a short code sequence that exhibits a couple of problems. The expected behaviour is:
-
The user hits the button, is presented with a textbox
-
On entering the text (and hitting return), the text appears in the tableview table and the textbox is dismissed
-
The user repeats the process
-
if the number of rows entered overflows the visible table, it scrolls to make the row visible.
Actual behaviour:
-
The native text box sometimes not dismissed. This is worrying since it indicates a bug somewhere and may account for other problems that I have been having, but not illustrated by this example.
-
The 4th row to be entered is not visible, but the function getRowAtIndex insist that it is. Not until 6 rows have been entered does the expected scrolling appear.
Is there a problem in calling nested event handlers (e.g. onRowRender from inside the nativeTextField handler from inside the button handler)?
[lua]
–
– main.lua
–
widget = require “widget”
_CW = display.contentWidth
_CH = display.contentHeight
local rowHeight = 60
local searchTable
----------------- function onRowRender ------------------
local function onRowRender (event)
local row = event.row
print("About to render row, index = ",row.index)
local rowText = display.newText(row, row.params.Name, 0, 0, nil, 14)
rowText:setFillColor( 0, 0, 0 )
rowText.x = _CW/2
rowText.y = rowHeight/2
return true
end – end onRowRender
local rowAtIndex
local numRows
local searchListener = function(event)
if (event.phase == “submitted”) then
searchText = event.target.text
searchTable:insertRow( {
rowHeight=rowHeight, params = {Name = event.target.text}
} )
– is last row visible, if not scroll to it
numRows = searchTable:getNumRows()
rowAtIndex = searchTable:getRowAtIndex(numRows)
if not rowAtIndex then
print(“entry off screen”)
searchTable:scrollToIndex(numRows)
else
print(“entry visible”)
end
native.setKeyboardFocus(nil)
event.target:removeSelf()
end – if submitted
return true
end – searchListener
-------------------------- button release listener -------------------
local addButtonRelease = function(event)
local searchField = native.newTextField(
_CW/2, _CH/2, _CW*0.8, 30)
searchField.font = native.newFont(nil,14)
searchField.placeholder = “enter name to search for”
native.setKeyboardFocus(searchField)
searchField:addEventListener(“userInput”, searchListener)
end – addButtonRelease
------------------------ generic button params ---------------
buttonParams = {
onRelease = addButtonRelease,
shape = “roundedRect”,
width = 100,
height = 30,
cornerRadius = 5,
fillColor = { default={1,1,0,1}, over={1,0.1,0.7,0.4} },
strokeColor = { default={1,0.4,0,1}, over={0.8,0.8,1,1} },
strokeWidth = 0,
fontSize = 10,
}
---------------------- add search button -------------------
local addButton = widget.newButton(buttonParams)
addButton.id = “addButton”
addButton.y = _CH - 10
addButton:setLabel( “add to search” )
------------ create table of search requests --------------
searchTable = widget.newTableView {
top = 30 ,
height = _CH/3,
onRowRender = onRowRender,
backgroundColor = {1, 1, 1, 0.5}
}
[/lua]