tableview insert and delete rows

Hello Gents… 

Maybe I am overlooking something, but I did not find way how to insert row into middle of the table (to some index value). Method tableview:insertRow is only adding row at the bottom of the table.

So, is there any way to insert row(s) for example at the top of the table?

Anyway I have also found that if I delete row in the middle of the table by tap and using method deleteRows, table indexes are not moved. (tableview is not reindexed). If I for example remove 3rd row in the table from 5 rows total,  then indexes of rows in the table for touch/tap (event.row.index, event.target.index) remains like {1,2,4,5}. I deleted 3rd but indexes wan not shifted.

Is it correct behaviour, or am I missing something?

Thnx

Michael

You are not missing any thing. If I’m being honest and in my experience, it’s almost easier to delete all the rows and re-insert them. The entire visible tableView will be rendered in a single frame update so it’s not noticeable to do that unless you’re inserting hundred of records, which may not be a best practice anyway.

Rob

Thank you for helping even during weekend :-). Finally I did it exactly that way. I recreated table from the scratch and it works great. But in another scene I need (better said I would like) to populate tableview with couple of hundreds of items. There will be also search textField on the top of scene but users may not know what are they search for, and in this case they should have possibility to list through the all items in the table. What it the best practice to populate large amount of items into tableview? Let’s say I have 20 items visible on screen. Would it be possible when scene appears load into table let’s say 50 items, and then in different thread on background check for last visible table row and … if (numberOfRowsActuallyPopulated - currentLastVisibleRowIndex) < 15 then insertIntoTableAnother20Rows? Will this break user table scrolling experience? Does someone did this or have code snippet for this?

thnx

Michael

To me, it’s important to think of a tableView in the MVC or Model-View-Controller model. It’s just the view of the data. It should not be used as a data management tool.  If you do a search and come back with 200 records, instead of loading 200 records into the tableView, load 50 and when you detect you’ve scrolled to the bottom, insert the next 50 records, kind of like endless scroll on the Facebook App.

For instance, we made a sample app called CoronaWeather (https://github.com/coronalabs-samples/CoronaWeather)

In it I have a SQLite database of locations, with hundreds of thousands of city, latitude and longitude data.  I setup a search bar, which actually uses a tablleView to display the search matches.  Imagine typing in “San”. In early versions I was crushing the tableView tying to insert ever city that beings with “San” in it. In my case I chose to only load up a fixed number of items only after the filter reduced the total search results to a manageable amount. No one is going to scroll through 400 cities that start with “San” looking for San Joee. They can type in "San J"and get fewer records to look with.

But the key point I want to make is that the data look up happens on the SQL database and not the tableView. The tableView is there to only show the hand full of selected records and the code (the controller in this example) is responsible for deciding the number of records to insert into the view.

Take a look at what I did with the CoronaWeather app.  All the stuff is happening in location.lua.

Rob

Yes, I was quickly looking into this code before, but you are starting populate table AFTER two characters for search in data typed. That will narrow table data much. But if you don’t know what are you looking for (for weather you at least know name of city or region etc… ), I want to leave users to go through data, to really see all hundreds of items. Problem is that corona tableview implementation is not MVC model. If your implementation would actually create table rows in “onRowRender” function, it would be MVC, but now all table rows you want to display have to be in the table before you actually render them. That is main the difference between iOS implementation of UITableview and yours. But I want to write multiplatform app so objC or Swift are for me out of question. And I also refuse to learn JAVA for android :-), so I have chosen Corona as compromise. Ideally I would like to use python, but is it not possible, :slight_smile: . Anyway, Lua is super easy, but for business app you need put a lot of additional work to make your widgets work as native… I am not complaining Corona is great, has good learning curve and is as I wrote supereasy (for example use transitions are a lot of easier to learn as in native app) and I know that is used for 2D games mostly…  

So I will try to use coroutines and I will write how it works… :slight_smile:

Thnx anyway

Michael

update: I tried (couple of hours) to make it work through scrollevent listener, then I simply tried to load entire table at once, and it was quick (only text), so I found I don’t need this functionality for now :slight_smile:

You are not missing any thing. If I’m being honest and in my experience, it’s almost easier to delete all the rows and re-insert them. The entire visible tableView will be rendered in a single frame update so it’s not noticeable to do that unless you’re inserting hundred of records, which may not be a best practice anyway.

Rob

Thank you for helping even during weekend :-). Finally I did it exactly that way. I recreated table from the scratch and it works great. But in another scene I need (better said I would like) to populate tableview with couple of hundreds of items. There will be also search textField on the top of scene but users may not know what are they search for, and in this case they should have possibility to list through the all items in the table. What it the best practice to populate large amount of items into tableview? Let’s say I have 20 items visible on screen. Would it be possible when scene appears load into table let’s say 50 items, and then in different thread on background check for last visible table row and … if (numberOfRowsActuallyPopulated - currentLastVisibleRowIndex) < 15 then insertIntoTableAnother20Rows? Will this break user table scrolling experience? Does someone did this or have code snippet for this?

thnx

Michael

To me, it’s important to think of a tableView in the MVC or Model-View-Controller model. It’s just the view of the data. It should not be used as a data management tool.  If you do a search and come back with 200 records, instead of loading 200 records into the tableView, load 50 and when you detect you’ve scrolled to the bottom, insert the next 50 records, kind of like endless scroll on the Facebook App.

For instance, we made a sample app called CoronaWeather (https://github.com/coronalabs-samples/CoronaWeather)

In it I have a SQLite database of locations, with hundreds of thousands of city, latitude and longitude data.  I setup a search bar, which actually uses a tablleView to display the search matches.  Imagine typing in “San”. In early versions I was crushing the tableView tying to insert ever city that beings with “San” in it. In my case I chose to only load up a fixed number of items only after the filter reduced the total search results to a manageable amount. No one is going to scroll through 400 cities that start with “San” looking for San Joee. They can type in "San J"and get fewer records to look with.

But the key point I want to make is that the data look up happens on the SQL database and not the tableView. The tableView is there to only show the hand full of selected records and the code (the controller in this example) is responsible for deciding the number of records to insert into the view.

Take a look at what I did with the CoronaWeather app.  All the stuff is happening in location.lua.

Rob

Yes, I was quickly looking into this code before, but you are starting populate table AFTER two characters for search in data typed. That will narrow table data much. But if you don’t know what are you looking for (for weather you at least know name of city or region etc… ), I want to leave users to go through data, to really see all hundreds of items. Problem is that corona tableview implementation is not MVC model. If your implementation would actually create table rows in “onRowRender” function, it would be MVC, but now all table rows you want to display have to be in the table before you actually render them. That is main the difference between iOS implementation of UITableview and yours. But I want to write multiplatform app so objC or Swift are for me out of question. And I also refuse to learn JAVA for android :-), so I have chosen Corona as compromise. Ideally I would like to use python, but is it not possible, :slight_smile: . Anyway, Lua is super easy, but for business app you need put a lot of additional work to make your widgets work as native… I am not complaining Corona is great, has good learning curve and is as I wrote supereasy (for example use transitions are a lot of easier to learn as in native app) and I know that is used for 2D games mostly…  

So I will try to use coroutines and I will write how it works… :slight_smile:

Thnx anyway

Michael

update: I tried (couple of hours) to make it work through scrollevent listener, then I simply tried to load entire table at once, and it was quick (only text), so I found I don’t need this functionality for now :slight_smile: