Search code examples
luacoronasdkcorona-storyboard

issue with list TableView in Corona


Am trying to do an App in Corona that will display list of topics/items in table view but i have been on it for over 2 weeks now, i cant get the list displayed on the App but its showing the last item on the list instead of listing all on the tableView

i have tried "listRec[event.index].name" in place of "listRec.name" but am getting an error, please help me See my code below

    local widget = require( "widget" )
    listRec = {}
    local nameData =            {"System1","System2","System3","System4","System5","System6"
    ,"System7","System8","System9"}
    local list = nil

    local function loadData()
     for x=1, #nameData do
        listRec[x] = {}
        listRec.name = nameData[x]
        end
    end

    local function onRowRender( event )

        -- Get reference to the row group
        local row = event.row
        local rowGroup = event.view

        -- Cache the row "contentWidth" and "contentHeight" because the row bounds can change       as children objects are added
        local rowHeight = row.contentHeight
        local rowWidth = row.contentWidth

        local rowTitle = display.newText(row, listRec.name, 0, 0, nil, 35 )
        rowTitle:setFillColor( 0 )

     -- Align the label left and vertically centered
        rowTitle.anchorX = 0
        rowTitle.x = 10
     rowTitle.y = rowHeight * 0.5

    end --onRowRender


    local function pageup()

    -- Create the widget
        tableView = widget.newTableView
    {   
            top = 50,
            height = screenHeightSB,
            width = screenWidth,
            onRowRender = onRowRender,
            onRowTouch = onRowTouch,
            listener = scrollListener
    }

    --Heading Outline
        local heading = display.newText("Course Outline", 0,0, "Helvetica" or       "native.systemFont", 40)
        heading.x = centerX
    heading.y = 25
    end ---pageup


    local function showRec()
        -- Insert 40 rows
        for i = 1, #listRec do
        local rowHeight = 60
         -- Insert a row into the tableView
         tableView:insertRow{
        rowHeight = rowHeight
        }
        end
    end --- showRec

    loadData()
    pageup()
    showRec()

Solution

  • You can modify your code as the following:

    local function loadData()
         for x=1, #nameData do
            listRec[x] = {
            name = nameData[x]
            }
            end
        end
    

    then inside the rowRender function you add

     local idx = row.index
     local rowTitle = display.newText(row, listRec[idx].name, 0, 0, nil, 35 )
    

    That should do it I guess