I’m having an issue with a Tableview where the ID of the rows doesn’t get updated after a row has been deleted with deleteRows. I’ve tried to reload the table but it does not help. It seems someone else had the issue a couple of years ago and it doesn’t seem to be fixed?
Below you can find a full working example showing the problem. Sorry I couldn’t make it smaller.
local composer = require( "composer" ) local scene = composer.newScene() local widget = require "widget" local globalData = require( "globalData" ) local tableView local deleteButton local function confirmDeleteFile( event ) if ( event.action == "clicked" ) then local i = event.index if ( i == 1 ) then local result, reason = os.remove( system.pathForFile( globalData.fileTapped, system.DocumentsDirectory ) ) tableView:deleteRows( { globalData.rowIndexTapped } ) tableView:reloadData() end return true -- indicates successful touch end end local function onDeleteButtonRelease(event) local alert = native.showAlert( "Delete saved game", "Are you sure you want to delete this game?", { "Yes", "No, cancel" }, confirmDeleteFile ) return true -- indicates successful touch end local function onRowTouch(event) print("onRowTouch (event.target.index): "..event.target.index) local row = event.target if ( event.phase == "release" or event.phase == "tap" ) then globalData.fileTapped = event.target.params.file globalData.rowIndexTapped = event.target.index deleteButton:addEventListener("tap", onDeleteButtonRelease) for i = 1, tableView:getNumRows() do if i == row.index then tableView.\_view.\_rows[i].\_rowColor.default = { 1, 100/255, 100/255 } else local indexVisibleLine = tableView:getRowAtIndex( i ) -- This makes sure the line is visible. If not visible, \_view.\_rows[i] will crash if ( indexVisibleLine ) then tableView.\_view.\_rows[i].\_rowColor.default = { 232/255, 232/255, 232/255 } end end end tableView:reloadData() return true -- indicates successful touch end end function scene:create( event ) local sceneGroup = self.view local function onRowRender( event ) local row = event.row local rowTitle = display.newText( row, event.row.params.file, 0, 0, nil, 100 ) rowTitle:setFillColor( 0 ) rowTitle.anchorX = 0 rowTitle.x = 0 rowTitle.y = 56 -- half of 112 (current rowHeight) end tableView = widget.newTableView( { left = 0, top = 0, height = display.contentHeight-400, width = display.contentWidth, onRowRender = onRowRender, onRowTouch = onRowTouch, listener = scrollListener } ) local lfs = require ( "lfs" ) local path = system.pathForFile( "", system.DocumentsDirectory ) local savedGames = {} for file in lfs.dir ( path ) do if string.find( file, ".db" ) then table.insert( savedGames, file ) tableView:insertRow{ rowHeight = 112, params = {file=file}, -- Include custom data in the row rowColor = { default = { 255, 255, 255}, over = { 255, 0, 0}, }, } end end deleteButton = widget.newButton( { width = 200, height = 100, defaultFile = "button.png", overFile = "button-over.png", fontSize= 60, labelColor = { default={255}, over={128} } } ) deleteButton.x = display.actualContentWidth/1.5 deleteButton.y = display.actualContentHeight\*0.9 deleteButton:setLabel( "Delete" ) sceneGroup:insert( deleteButton ) sceneGroup:insert( tableView ) end function scene:destroy( event ) local sceneGroup = self.view deleteButton:removeSelf() -- widgets must be manually removed deleteButton = nil tableView:removeSelf() tableView = nil end scene:addEventListener( "create", scene ) scene:addEventListener( "destroy", scene ) return scene
Tapping on each row after a deletion will print:
onRowTouch (event.target.index): 1
onRowTouch (event.target.index): 2
onRowTouch (event.target.index): 4
onRowTouch (event.target.index): 5
onRowTouch (event.target.index): 6
If someone can confirm this is a bug I can file a bug report.
