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.
Interaction between classes
-
I have a strange situation that I would like to get some help with. I have written a script with a class that contains a set of widgets that are common to several views so I want the common widgets to appear in the same place in any of the views. For the example code below, I have just one widget, a textview. When I type into the textview, I want a tableview dropdown to appear with a selection from a list that matches the text in the textview. When I select a row from the textview, I want the tableview delegate action to populate the textview with the selected row from the tableview and then have the tableview be hidden. The code below almost does this. With line 7 (#main.author_result.text = self.items[row]) commented out, once the row is selected from the tableview the tableview is hidden but the textview isn't populated with the tableview row. If I uncomment line 7, when I select a row from the tableview, the textview is populated properly but the tableview isn't hidden. This seems like very strange behavior. Can someone advise me how to correct the code to work as described above? Thanks.
import ui class MyTableViewDelegate(object): def __init__(self,items): self.items = items def tableview_did_select(self, tableview, section, row): #main.author_result.text = self.items[row] tableview.hidden = True pass class MyAuthorDelegate (object): def textview_did_change_selection(self, textview): author_unique = ['Aaaa', 'Aabb', 'Bbbb', 'Bccc'] length = len(textview.text) options = [] for author in author_unique: if author[:length] == textview.text: options.append(author) datasource = ui.ListDataSource(options) dropDown = ui.TableView() dropDown.data_source = datasource dropDown.delegate = MyTableViewDelegate(options) dropDown.x = 100 dropDown.y = 55 dropDown.width = 250 dropDown.row_height = 25 view.add_subview(dropDown) pass class Common_View (object): def __init__(self,*args,**kwargs): # pass self.author_result = ui.TextView(name='author',frame=(100,5,250,40), border_color='black', border_width=3, text= '', font=('Arial Rounded MT Bold',20)) # w, h = ui.get_screen_size() view = ui.View(name='Test', bg_color='lightblue', frame=(0, 0, w, h)) main = Common_View() view.add_subview(main.author_result) main.author_result.delegate = MyAuthorDelegate() nav_view = ui.NavigationView(view) nav_view.present('sheet')```
-
@frankL Is that normal that in the TextView delegate, you create each time a new TableView? Without deleting the old one.
-
I thought I was just updating the tableview each time. If I'm creating a new one each time then that could explain why it's not hidding. It is hiding but is creating a new one. How do I just create the one and then just update it each time textview changes?
-
@frankL try something like
import ui class MyTableViewDelegate(object): def tableview_did_select(self, tableview, section, row): main.author_result.text = tableview.data_source.items[row] tableview.hidden = True class MyAuthorDelegate (object): def textview_did_change_selection(self, textview): author_unique = ['Aaaa', 'Aabb', 'Bbbb', 'Bccc'] length = len(textview.text) options = [] for author in author_unique: if author[:length] == textview.text: options.append(author) view['dropDrown'].data_source.items = options view['dropDrown'].hidden = False class Common_View (object): def __init__(self,*args,**kwargs): # pass self.author_result = ui.TextView(name='author',frame=(100,5,250,40), border_color='black', border_width=3, text= '', font=('Arial Rounded MT Bold',20)) # w, h = ui.get_screen_size() view = ui.View(name='Test', bg_color='lightblue', frame=(0, 0, w, h)) main = Common_View() view.add_subview(main.author_result) main.author_result.delegate = MyAuthorDelegate() dropDown = ui.TableView(name='dropDrown') datasource = ui.ListDataSource([]) dropDown.data_source = datasource dropDown.delegate = MyTableViewDelegate() dropDown.x = 100 dropDown.y = 55 dropDown.width = 250 dropDown.row_height = 25 view.add_subview(dropDown) nav_view = ui.NavigationView(view) nav_view.present('sheet')
-
That works perfectly! Thank you. As always, I really appreciate your help.