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.
TableView cell issue
-
I have a TableView and it initializes with one cell. I don’t know how to get rid of that cell.
class tvDelegate(object): #also acts as the data_source. Can be separate, but this is easier. def __init__(self,items): self.items = items self.currentNumLines = len(items) self.currentTitle = None self.currentRow = None def tableview_did_select(self, tableview, section, row): # Called when a row was selected. self.currentTitle = self.items[row]['title'] self.currentRow = row # needed for the test above if self.items[row]['mediaType'] == 'photo': preview = v['view1'] photo = previewPhoto photo.image = self.items[row]['itemURL'] preview.add_subview(photo) photo.frame = preview.bounds tableview.reload_data() # forces changes into the displayed list def tableview_did_deselect(self, tableview, section, row): # Called when a row was de-selected (in multiple selection mode). pass def tableview_title_for_delete_button(self, tableview, section, row): # Return the title for the 'swipe-to-***' button. return 'Delete' # or 'bye bye' or 'begone!!!' def tableview_number_of_sections(self, tableview): # Return the number of sections (defaults to 1). Someone else can mess with # sections and section logic return 1 def tableview_number_of_rows(self, tableview, section): # Return the number of rows in the section return self.currentNumLines #needed to be in sync with displayed version, def tableview_cell_for_row(self, tableview, section, row): # Create and return a cell for the given section/row cell = ui.TableViewCell() cell.background_color = '#606060' cell.text_label.text = self.items[row]['title'] if self.items[row]['mediaType'] == 'photo': try: cell.image_view.image = self.items[row]['itemURL'] except AttributeError: pass return cell def tableview_title_for_delete_button(self, tableview, section, row): # Return the title for the 'swipe-to-***' button. return 'Delete' # or 'bye bye' or 'begone!!!' def tableview_can_delete(self, tableview, section, row): # Return True if the user should be able to delete the given row. return True # you can use logic to lock out specific ("pinned" entries) def tableview_can_move(self, tableview, section, row): # Return True if a reordering control should be shown for the given row (in editing mode). return True # see above def tableview_delete(self, tableview, section, row): # Called when the user confirms deletion of the given row. self.currentNumLines -=1 # see above regarding hte "syncing" tableview.delete_rows((row,)) # this animates the deletion could also 'tableview.reload_data()' del self.items[row] def tableview_move_row(self, tableview, from_section, from_row, to_section, to_row): # Called when the user moves a row with the reordering control (in editing mode). self.items = listShuffle(self.items,from_row,to_row) # cynchronizes what is displayed with the underlying list
-
@robertiii, its a little hard to see what you are trying to do here because only have part of the picture. Some times when you have dependencies and you dont want to list all the code. Can make a mini example of the code that just focuses on the problem you are having. Makes it so much easier for ppl to help if they can copy and run your code in Pythonista and see the problem.
But i think if you declare your delegate as below, should help. It's important not to set items=[] in the init method. It's a Python thing about mutable types that you can read about.
Another small thing. I would think about your data_source implementing the delegate. Not your delegate hosting your data_source. But I could be wrong on that. That's my feeling anyway. The object model feels more natural that way.
I hope I have understood your question correctly.
class tvDelegate(object): #also acts as the data_source. Can be separate, but this is easier. def __init__(self,items=None): self.items = items if items else []
-
self.items = items or []
# ;-) -
@ccc , lol you will either be the death or the life of me :)
Edit: But I have not seen that syntax before, I am sure I have, but not extrapolated to this use case :( -
I am total beginner. I have solved the issue but no idea how. I know enough to be dangerous. This TableView code came from an example on the forum.
-
@robertiii , I feel the same. I know enough to be dangerous :) but thats ok....The smart guys on this forum are pretty good. If they need to step in and correct us, they do.