Delete rows in tableview
I have a tableview created in a pyui file rather than in code. The data source is a csv file. I can delete a row in the tableview, but how can I then capture the event and be able to delete the respective item in the csv file.
Any help is appreciated. This forum is an extraordinary source of information.
@coomlata1 , this calls you back when a row is deleted. It's not exactly documented as far as I can see, but my feeling is it supposed to be used like this or at least can been with ui.ListDataSource which is what is automatically assigned when you create a table in the ui.designer. If you look at the data_source callbacks, you could also trap the other callbacks if you wanted or needed doing the same.
# coding: utf-8 import ui def tableview_delete(tableview, section, row): # Called when the user confirms deletion of the given row. print 'in delete', tableview, section, row # load v = ui.load_view('Table.pyui') tbl = v['tbl'] # get a ref to the table in the form # replace the tableview_delete function tbl.data_source.tableview_delete = tableview_delete v.present('sheet')```
tableview_delete()look like if I want to show the default visual behaviour and perform some additional action?
Take a look at ui.py in Standard Library/site-packages/ui.py, and look at the ListDataSource, which has a fairly complete implementation.
Be sure to implement
tableview_can_delete(self, tv, section, row):to give you the delete button when swiping left. then tableview_delete should call
to animate the deletion.
@omz ... does delete_rows take a section argument? Does this work with tableviews that have multiple sections?
@JonB Yes, you can pass a sequence of numbers or 2-tuples. If you pass tuples, they're interpreted as (row, section).
@omz Is there a reason why
delete_rowstakes the tuples in
(row, section)order instead of
(section, row)like everything else?
@dgelessus Yes, stupidity. ;) To be honest, I was surprised by that too, I think I did it this way because the corresponding ObjC objects are constructed with the method
Unfortunately, this gives me an Error. "Inconsistent number of rows after deletion.", @JonB
You need to ensure that your
tableview_number_of_rowsmethod returns the correct number, i.e. you must delete the row from your data before calling
Ah, thanks @omz - got it now!