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 refresh from dictionary
-
I want to refresh Tableview data from a dictionary by selecting a row in the Tableview. This code works fine the first time but then dies.
I’ve run into many errors: recursion, referenced before assigning, not defined, etc.
Can anyone suggest where I’ve gone wrong with my logic or my coding?
import ui, console view=ui.View() view.frame=(0,0,400,400) d1={ 'quantity':['energy','space'], 'space':['time','quantity'], 'time':['space','quantity'], } #------------------------------------ def cell_tapped1(sender): cellval1 = ds1.selected_row ll01=cps[cellval1] ll01=ll01.strip() cell_tapped2(ll01) #------------------------------------- def cell_tapped3(self): ds3=ui.ListDataSource(cpss) ds3.action=cell_tapped3 tv1.data_source=tv1.delegate cellval1 = ds3.selected_row ll01=cpss[cellval1] ll01=ll01.strip() cell_tapped2(ll01) #_#----------------------------------- def cell_tapped2(ll01): cpss=[] cpss.append(ll01) for i in d1[ll01]: cpss.append(' '+i) ds3=ui.ListDataSource(cpss) ds3.action=cell_tapped3 tv1.data_source=tv1.delegate=ds3 tv1.reload_data() #console.clear() for i in cpss: print (i) #----------------------------------------- tv1=ui.TableView() tv1.width,tv1.height=400,350 cps=[] cps.append('space') cps.append('time') cps.append('quantity') ds1=ui.ListDataSource(cps) ds1.action=cell_tapped1 tv1.data_source=tv1.delegate=ds1 view.add_subview(tv1) view.present('sheet')
-
@worldmale your problem is that you recreate each time a new DataSource. Don't do that
Anyway, I have to say that I don't understand what the script has to do.
Try
import ui, console view=ui.View() view.frame=(0,0,400,400) d1={ 'quantity':['energy','space'], 'space':['time','quantity'], 'time':['space','quantity'], } #------------------------------------ def cell_tapped1(sender): cellval1 = ds1.selected_row ll01=cps[cellval1] ll01=ll01.strip() cell_tapped2(ll01) def cell_tapped2(ll01): cpss=[] cpss.append(ll01) for i in d1[ll01]: cpss.append(' '+i) tv1.data_source.items = cpss tv1.reload_data() #console.clear() for i in cpss: print (i) #----------------------------------------- tv1=ui.TableView() tv1.width,tv1.height=400,350 cps=[] cps.append('space') cps.append('time') cps.append('quantity') ds1=ui.ListDataSource(cps) ds1.action=cell_tapped1 tv1.data_source=tv1.delegate=ds1 view.add_subview(tv1) view.present('sheet')
-
@cvp
Your suggestion helped, thank you, but I still have a problem to get the correct value of ‘ll01’.
The variable ‘cellval1’ is an integer. If I could get the text directly from the selected row I could get the correct value of ll01.
Or barring that, I need to refresh ds1 with current list cpss, to look up ll01 with the selected row number. -
@worldmale said
I need to refresh ds1 with current list cpss, to look up ll01 with the selected row number
That's what you do with
tv1.data_source.items = cpss tv1.reload_data()
If I could get the text directly from the selected row I could get the correct value of ll01.
def cell_tapped1(sender): #cellval1 = ds1.selected_row #ll01=cps[cellval1] #ll01=ll01.strip() ll01 = tv1.data_source.items[ds1.selected_row].strip() cell_tapped2(ll01)
Or even
ll01 = sender.items[sender.selected_row].strip()
-
Thank you for your help. Your suggestions helped me out of a dead end. I suspect you already know the value of a helping hand.
The key was your —tv1.data_source.items = cpss—suggestion. Even now, I can’t find the documentation.
There is tv1.ListDataSource. Items. Is this the same thing, different syntax? -
@worldmale you have defined tv1.data_source = ds1 and ds1 = ui.ListDataSource(), thus...,
And help is easy to get:
Then you get