I am using the widget.newTableView to create a display of my table. This works fine.
I have ID, name, type all displayed in fake columns (by manipulating the x and y coordinates).
So I move an object to a new table. I remove them in the widget table. Now I want to reload the displayed data (or, failing that, to rewrite the widget table).
One problem I had writing the code is that the id of the plant is not the same as the table row (because I have a category row). That is why I had to use newIDt (table) and newIDd (display) rather than just one newID. Even so if I add a second object, the print strings (and thus any property yanking I do from the table) match up the wrong name for the picture. Not sure how to deal with this.
The error I get when trying to reload is: for line 121 of this code: attempt to index a nil value in function ‘reloadData’
My code is below. I hope I got the formatting right for the forum and that some people can help me!
--table of objects: original set tableplants= {} tableplants[1] = {bpid="ID", bpname="Names", bptype="Type", } tableplants[2] = {bpid="01", bpname="banacharis", bptype="water", } tableplants[3] = {bpid="02", bpname="bazsword", bptype="water",} tableplants[4] = {bpid="03", bpname="breeds", bptype="water", } tableplants[5] = {bpid="04", bpname="bwaterlily", bptype="water", } tableplants[6] = {bpid="05", bpname="bbirdsnestfern", bptype="land", } tableplants[7] = {bpid="06", bpname="bbromeliad", bptype="land", } tableplants[8] = {bpid="07", bpname="bdutchmanspipe", bptype="land",} tableplants[9] = {bpid="08", bpname="bfern", bptype="land", } tableplants[10] = {bpid="09", bpname="bleaves", bptype="land", } --rendering function function onRowRender( event ) --Set up the localized variables to be passed via the event table local row = event.row local id = row.index local params = event.row.params row.bg = display.newRect( 0, 0, display.contentWidth, 60 ) row.bg.anchorX = 0 row.bg.anchorY = 0 row.bg:setFillColor( 1, 1, 1 ) row:insert( row.bg ) if ( event.row.params ) then row.bpidText = display.newText( params.bpid, 12, 0, native.systemFont, 30 ) row.bpidText.anchorX = 0 row.bpidText.anchorY = 0.5 row.bpidText:setFillColor( 0,0,.5) row.bpidText.y = 20 row.bpidText.x = 30 row.bpnameText = display.newText( params.bpname, 12, 0, native.systemFont, 30 ) row.bpnameText.anchorX = 0 row.bpnameText.anchorY = 0.5 row.bpnameText:setFillColor( 0,0,0 ) row.bpnameText.y = 20 row.bpnameText.x = 100 row.bptypeText = display.newText( params.bptype, 12, 0, native.systemFont, 30 ) row.bptypeText.anchorX = 0 row.bptypeText.anchorY = 0.5 if params.bptype=="water" then row.bptypeText:setFillColor( 0,.5,1) else row.bptypeText:setFillColor(0,.5,0) end row.bptypeText.y = 20 row.bptypeText.x = 350 row:insert( row.bpidText ) row:insert( row.bpnameText ) row:insert( row.bptypeText ) end return true end --Small plants table: this is in the section scene:show( event ) phase=="did" area. smallPlantsTable = widget.newTableView( { left = 730, top = 20, height = 500, width = 650, onRowRender = onRowRender, onRowTouch = onRowTouch, backgroundColor = { .66, .65, .77 }, listener = scrollListener }) mainGroup:insert(smallPlantsTable) -- Insert as many rows as the table "rowText" for i = 1, #tableplants do smallPlantsTable:insertRow{ rowHeight = 60, isCategory = false, rowColor = { 1, 1, 1 }, lineColor = { 0.80, 0.80, 0.80 }, params = { bpid = tableplants[i].bpid, bpname = tableplants[i].bpname, bptype = tableplants[i].bptype, } } -- Make some rows categories if ( i == 1 ) then isCategory = true rowHeight = 40 rowColor = { default={0.8,0.8,0.8} } lineColor = { 1, 0, 0 } text=native.systemFontBold, 40 -- This doesn't seem to do anything! I want the categories to be bold. end end end end tablehandplants= {} --this is the new table to move plants into local function createPlant1() local newIDt = math.random(2,#tableplants) newIDd=newIDt-1 local newPlant1 = display.newImageRect( mainGroup, objectSheet1, newIDd, 100, 100 ) newPlant1.x, newPlant1.y = 100, 120 newPlant1.id = ( "Plant "..newIDd.."is "..newIDt.." in table") newPlant1.name= (tableplants[newIDt].bpname) print("Plant "..newIDd..", "..newPlant1.name.." was displayed") table.insert( tablehandplants, newIDd) print("Entry Added. TableHandPlants now has " .. #tablehandplants .. " entries") print ("TablePlants has " ..#tableplants .. " entries") table.remove(tableplants, newIDt) print ("Entry removed. TablePlants now has " ..#tableplants .. " entries") end --this is the game start. Clicking on a button called StartButton calls this function: local function startGame() createPlant1() --refresh table smallPlantsTable.reloadData() print ("Reloaded table") end