Moving rows on TableView

I have a TableView with several rows, in the onTouch event of each row, i show an alert with the options “Move Up” and “Move Down”, that moves the tapped row.

To do that, i switch the position of the rows on the table from which the TableView takes what is to be show. Like this:

for k,v in pairs(savedTable) do  --this if is to find the touched row if (v.code == row.params.code) then                        local temp = savedTable[k]       savedTable[k] = savedTable[k-1]     savedTable[k-1] = temp       break     end end   tableView:reloadData( )

I’m saving that “savedTable” in a json file.

Thing is, the reloadData() does nothing here. But if i relaunch/restart the project/app, the tableView is loaded with the changed positions.

If i print the savedTable before and after the for, i see that the position of the row has changed.

I change the text of the rows in a similar way, and the reloadData() works. Why it does not work for moving the rows? As i said, the code is moving then, the reload is just not updating the tableView.

I just want to be able to move the rows up or down, is there another way to do this?

Thanks

You don’t have access to the internal structure that maintains the tableView’s row ordering. There is no API level way to do what you’re asking. The easiest thing to do would be to change the order of your data, delete all the rows from the table and re-insert them in the order you want them. This seems aggressive, but it should be fast enough to  happen in a single frame so there shouldn’t be any flicker or UI hiccups.

Rob

Yeah thats what i’m doing.

Just hoped there would be a better way to do this. Need to see if that will be smooth on a big list.

Thanks.  :smiley:

Doing a bunch of inserts in a tight loop will block and make the UI non-responsive for a bit. This might be a good use of coroutines.

Rob

I’ll sure look into that.

Thanks.

You don’t have access to the internal structure that maintains the tableView’s row ordering. There is no API level way to do what you’re asking. The easiest thing to do would be to change the order of your data, delete all the rows from the table and re-insert them in the order you want them. This seems aggressive, but it should be fast enough to  happen in a single frame so there shouldn’t be any flicker or UI hiccups.

Rob

Yeah thats what i’m doing.

Just hoped there would be a better way to do this. Need to see if that will be smooth on a big list.

Thanks.  :smiley:

Doing a bunch of inserts in a tight loop will block and make the UI non-responsive for a bit. This might be a good use of coroutines.

Rob

I’ll sure look into that.

Thanks.