Forum Archive

How to populate designer tableview sections with a dictionary

nvedwards

I’m trying to populate a tableview with the contents of a dictionary. i want the tableview data to be grouped by sections following the rows belonging to each section. Below is the code that i put together so far. The keys show up in the tableview but not the values. How do you populate the rows/cells of each section with the key values?

Thanks!

import ui


def load_table(view):
    #data = ['spam', 'ham', 'egg']
    # Sections and section rows
    data = {'meat': ['spam', 'ham'], 'fruit': ['apple', 'orange']}

    datasource = ui.ListDataSource(data)
    view['tv_food'].data_source=datasource
    view['tv_food'].delegate=datasource


def main():
    view = ui.load_view('tableview_test') # tableview from designer
    load_table(view)
    nav = ui.NavigationView(view)
    nav.present()


if __name__ == '__main__':
    main()
cvp

@nvedwards see an example here or here

cvp

@nvedwards quick and dirty script only to show that it could be easier with dialogs

import dialogs

def main():
    # Sections and section rows
    data = {'meat': ['spam', 'ham'], 'fruit': ['apple', 'orange']}

    sections = []
    for k in data.keys():
        fields = []
        for val in data[k]:
            fields.append({'title':val})
        sections.append((k,fields))
    f = dialogs.form_dialog('test via dialogs',sections=sections)

if __name__ == '__main__':
    main() 
nvedwards

Very helpful. Thanks much!