Search and Display bar

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?

Hi @aaron34.  Since you appear to be a Starter account, you are probably running the last public build.  The widget library has had quite a few bugs fixed since them and they are available to Pro subscribers through the daily builds.  Because there were quite a few bugs that needed fixed when the cutoff for the public build happened, we made the source for the widgets open source, which means that you can download the code and tinker with it yourself.  But more importantly, as a Starter without access to public builds, you have access to the bugs we have fixed since then.

Go to https://github.com/coronalabs/framework-widgets/

And download the code there.  You will want to put the “widgetsLibrary” folder in the same folder with your main.lua with all of its contents in the right place and then put the widget.lua file that comes with it in the folder with your main.lua.  It helps to maybe rename it widget2.lua and require it:

    local widgets = require(“widgets2”)

instead of

    local widgets = require(“widgets”)

and see if that addresses your problem.

Hi @aaron34.  Since you appear to be a Starter account, you are probably running the last public build.  The widget library has had quite a few bugs fixed since them and they are available to Pro subscribers through the daily builds.  Because there were quite a few bugs that needed fixed when the cutoff for the public build happened, we made the source for the widgets open source, which means that you can download the code and tinker with it yourself.  But more importantly, as a Starter without access to public builds, you have access to the bugs we have fixed since then.

Go to https://github.com/coronalabs/framework-widgets/

And download the code there.  You will want to put the “widgetsLibrary” folder in the same folder with your main.lua with all of its contents in the right place and then put the widget.lua file that comes with it in the folder with your main.lua.  It helps to maybe rename it widget2.lua and require it:

    local widgets = require(“widgets2”)

instead of

    local widgets = require(“widgets”)

and see if that addresses your problem.