Edit: I made it work by adding a call to buildingTableView.reloadData() after changing the color of the old row. However, now sometimes when I drag the table view up or down I get the following error:
20:20:32.377 ERROR: Runtime error 20:20:32.377 ?:0: attempt to call method 'setFillColor' (a nil value) 20:20:32.377 stack traceback: 20:20:32.377 ?: in function 'touch' 20:20:32.377 ?: in function '?' 20:20:32.377 ?: in function \<?:182\>
This happens even with all calls to setFillColor commented out in my code.
Okay, I’m trying to work out how to use table views and I’m either making a dumb mistake or having a fundamental misunderstanding of how what I’m trying to do works. What I want to do is to have the last row tapped in my table view have a different background color than the other rows to indicate that it’s currently selected. For this I have the following row touch listener:
local function onBuildingRowTouch(event) local row = event.target local params = row.params if event.phase == "tap" or event.phase == "press" then --change row colors if oldBuildingRow ~= nil and oldBuildingRow.index ~= row.index then --print("Old row index: "..oldBuildingRow.index) oldBuildingRow:setRowColor({default = backgroundColor, over = overColor}) end row:setRowColor({default = selectedColor, over = overColor}) oldBuildingRow = row end end
where oldBuildingRow is a file-level variable. What’s supposed to happen is that the old highlighted row, if there is one, is reset to the default backgroundColor. Then, the newly pressed row is highlighted with selectedColor. Finally I store a reference to the current row in oldBuildingRow for the next time the listener is called.
What actually happens is that a given row does not un-highlight when another row is selected. It’s as if the reference to the old row is no longer “active,” even though the index and all the params and everything can still be accessed from the oldBuildingRow variable.
Does anybody know what I’m doing wrong?
Also, is there any way to distinguish a tap event from a drag? Currently the above triggers when I drag to move the table view up and down (the row where I begin the drag is selected). Ideally I would like it to trigger only when the user touches the row and does not drag afterwards.
Thanks!