I’ve got yet another question. I’m trying to assign a variable to the index of the row so I can use it in a wider scope, but I’m having some issues. Let me show you guys the code first:
function functions.onRowTouch( event ) print("ONROWTOUCH EXECUTING") if event.phase == "tap" or event.phase == "press" then print("ITEM tapped") if saveData.names[event.target.index] ~= nil then editItemNum = event.target.index print("editnum: " .. editItemNum) function functions.pressRow(event) print("ROW NUM: " .. event.target.index) print("EDITNUM = " .. editItemNum) functions.pressItem() return true end event.target:addEventListener("tap", functions.pressRow) end elseif event.phase == "swipeLeft" then print("SWIPING LEFT") tableView:deleteRows({event.target.index}, {slideLeftTransitionTime=450}) table.remove(saveData.names, editItemNum) table.remove(saveData.dates, editItemNum) table.remove(saveData.amounts, editItemNum) loadsave.saveTable(saveData, "payMeBackTable.json") print("ROW DELETED") end end
The code works great for the first time an item gets tapped, but after that “event.target.index” becomes nil because the print statement is not executing anymore. So I tried the following code:
function functions.onRowTouch( event ) print("ONROWTOUCH EXECUTING") if event.phase == "tap" or event.phase == "press" then editItemNum = event.target.index print("editnum: " .. editItemNum) print("ITEM tapped") if saveData.names[event.target.index] ~= nil then function functions.pressRow(event) print("ROW NUM: " .. event.target.index) --editItemNum = index print("EDITNUM = " .. editItemNum) functions.pressItem() return true end event.target:addEventListener("tap", functions.pressRow) end elseif event.phase == "swipeLeft" then print("SWIPING LEFT") tableView:deleteRows({event.target.index}, {slideLeftTransitionTime=450}) table.remove(saveData.names, editItemNum) table.remove(saveData.dates, editItemNum) table.remove(saveData.amounts, editItemNum) loadsave.saveTable(saveData, "payMeBackTable.json") print("ROW DELETED") end end
Note that the assignment of “editItemNum” is placed outside the if saveData… condition. The print statement was executed this time. So let’s say I’ve got 3 items
- Item1
- Item2
- Item3
When I delete the second item, the third item should become item2, that seems pretty obvious. But when I now tap on this item highlighted in red
- Item1
- Item2(3)
It still prints that it is item number 3.
So I tried to explain it as well as possible, please let me know if you don’t understand something about my explanation and ofcourse, let me know if you can help me with this problem!