I’ve got a long list of options for the user of my app to pick from which I have display in a tableView. However, this list is so long that in order to pick any of the options at the bottom of the list, the user must scroll down the page for several seconds. What I’d like to do is put in a search bar at the top of the screen. As the user types, only the options in the list that contain the exact string that the user type should pop up. I’ve been trying to figure out how to do this, and haven’t come up with anything that works. My best attempt is as follows:
[lua]
–other code
function scene:enterScene(event)
--code to create a native.newTextField called “searchBar”
searchCompare = searchBar.text
local sort = function(event)
if event.phase == “editing” then
--remove existing tableView
listOfTypes =
{
--empty
}
local n = 1
local thispath = system.pathForFile(“options.txt”, system.ResourceDirectory)
for line in io.lines(thispath) do
if string.match(line, searchBar.text) then
listOfTypes[n] = line
n = n + 1
end
end
local tableView = widget.newTableView
{
width = display.contentWidth,
height = display.contentHeight,
top = display.contentHeight/10,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
}
-------------------------------------------------------------
for i = 1, table.maxn(listOfTypes) do
local isCategory = false
local rowHeight = display.contentHeight/10
local rowColor =
{
default = { 255, 255, 255 },
}
local lineColor = { 220, 220, 220 }
tableView:insertRow
{
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
}
end
end
searchBar:addEventListener(“userInput”, sort)
end
[/lua]
When I try this, I get an error saying that I’m trying to do arithmetic on “contentHeight” which is a nil value. However, I’m not sure what it’s referring to because it doesn’t give me a line number. Any suggestions?