Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Deleted row index for ListDataSource
-
I'm just wondering if there's something really obvious that I'm missing.
I have a custom view class with a
tableview
and thetableview.data_source
is aui.ListDataSource
.What I would like to do is handle deletion of items from the list. I know that we have this
edit_action
but I think the issue I'm having is, how can I pass on or get the deleted row index? I can get this information forselected_row
but I can't get it fordeleted_row
.(And no,
selected_row
doesn't work because you can delete something other than the selected row...!)Example:
def selected(sender): selected_row = sender.selected_row def delete_row(sender): deleted_row = sender.deleted_row #something like this doesn't exist
I have tried variations of setting the delegate etc. Maybe there's something I could do there...not sure. But so far, hasn't worked.
At the moment I have settled for using
edit_action
, getting the difference between a before and after list to determine the row, and then going from there.Anyway... I'm doing a bit of gymnastics with this code now and it's possible I just need to do something like sitting down.
Always always....thanks for your help!
-
@cook , this is not an answer. I don't delete things from lists so not sure the correct way is. I am sure there is one. But until you get the real answer you could dynamically add your own attr deleted_row and maintain it.
When I am using the data_source, I often add a items attr to the TableView object as its normally readily available
As I say, I am sure it's not the best advise. But adding attrs to existing objects can be helpful -
# coding: utf-8 import ui class MyTableViewDataSource (object): def __init__(self, row_height): self.row_height = row_height self.width = None def tableview_number_of_rows(self, tableview, section): return len(tableview.data_source.items) def tableview_cell_for_row(self, tableview, section, row): self.width, height = ui.get_screen_size() cell = ui.TableViewCell() label = ui.Label() label.frame = (0,0,self.width,self.row_height) label.border_color = 'red' label.border_width = 1 label.text = tableview.data_source.items[row] label.alignment = ui.ALIGN_CENTER cell.content_view.add_subview(label) return cell def tableview_can_delete(self, tableview, section, row): return True def tableview_delete(self, tableview, section, row): print 'Delete row ' + str(row) del tableview.data_source.items[row] tableview.reload() class MyTableViewDelegate (object): def tableview_title_for_delete_button(self, tableview, section, row): return 'Delete me' class MyTableView(ui.View): def __init__(self): self.select_color = 'lightgrey' self.unselect_color = 'white' self.tv = ui.TableView() self.tv.row_height = 50 self.tv.data_source = MyTableViewDataSource(self.tv.row_height) self.all_items = ['1', '2', '3'] self.tv.data_source.items = self.all_items self.name = 'TableView-Test' self.tv.delegate = MyTableViewDelegate() self.tv.allows_selection = True self.add_subview(self.tv) self.present('full_screen') def layout(self): self.tv.reload() self.tv.frame = (0, 0, self.width, self.height) MyTableView()
-
@brumm @Phuket2 thanks for the reply. I was afraid the only real (built in) way to do it was to construct the data source as @brumm has shown. But, I don't need to have all this extra stuff around... perhaps I'll leave it as is....!
The other nice thing about the
ListDataSource
is that you can have multiple lines displayed in the cell (which I want!). It seems we are limited to two lines intableview_cell_for_row
construction.So perhaps I will stick with my gymnastics for now. I might need to change it later.... but expect to see me in the Olympics.
-
@cook , when using tableview_cell_for_row you can add extra ui.Labls to the cells content_view if you want/or anything else for that matter. Just that you recon charge of the sizing and placement of them. A bit more work.
-
@cook , maybe you know mvc (model, view, controller) all to well. I don't. I am struggling at the moment how to bring pieces of tiny app together. I am doing a lot of gymnastics, I am ready for Rio :) but then I am second guessing my self each time I make a decision and refactor. Basically, I get caught recursive of self doubt 😱
Anyway, I just seen this this article, mind you I have seen similar before. And while its fresh in my mind, I am going to try and refactor.
As I say, you may know this back the front. But it made me think of this thread also.
Here is the Article Link
Edit....
Come to think of it, I have seen @omz use this approach in his Python modules, like dialogs -
@phuket2 thanks for the link. I will read up on that later. Looks like a nice article!
I had no idea about the MVC method, to be honest. I think though that I was wondering about this sort of thing in particular about this project I am doing. Was trying to understand how to lay out everything. I know that I could do everything in a custom view class, but I felt as if that would be too difficult to navigate the code.
So I split up into a few classes and it seems okay.
One class is for dealing with the data (mostly SQL management)
Then I have my UISo perhaps I have a sort of MVC thing going on - but my C is mixed in with my V. My UI class has actions that control my data. I don't see a point to make a separate thing just to put those actions, especially when it's just a few lines of code for some.
Anyway... Will read through the article more. I think it's very helpful to know how to organize your code. I'm able to do that better now that I have a better handle on what's going on. I've learned some nice gymnastics. Might get bronze. Let's see.