Forum Archive

tableview_did_select

Appletrain

I’m launching a standard dialog from tableview_did_select

def tableview_did_select(self, tableview, section, row):
        self.selected_row = row
        self.selected_section = section
        print(dialogs.list_dialog('list dialog', items=['happy', 'days', 'are', 'here', 'again']))

This always returns None!

However, if it’s launched from a ui.ButtonItem on the same view it works properly and returns the selected item.

def cancel_action(sender):     
    print(dialogs.list_dialog('list dialog', items=['happy', 'days', 'are', 'here', 'again']))    
    ui.end_editing()
    ds.container_view.close()

This always returns the selected item

Any suggestions on making tableview_did_select work?

mikael

@Appletrain, please use the friendly code button `` to surround code with backticks and make it much more readable.

So the dialog comes up, you make the selection normally, but the return value is None?

Very curious: if you put the dialog call in another function that you call from did_select with ui.delay (very small delay), it works.

cvp

@Appletrain put @ui.in_background just before the def

@ui.in_background   
def tableview_did_select(self, tableview, section, row): 
mikael

@cvp, cool!

But then I do not really understand why ui.delay and the button action handler worked, as they are both run on the main UI thread, as is tableview_did_select.

cvp

@mikael I don't understand too but I often (not always) meet this problem.
The problem comes from the wait_modal in the dialog, it doesn't wait sometimes

stephen

@cvp @mikael this question is me trying to understnd.

is it posible the dialoge is making a synchronus call? or possible the dialoge delegate method is retuning before the diloge skipping dialoge return all together?

cvp

@stephen I think it is something like that..

stephen

@mikael @cvp in this old example off github does not have this problem..

only big difference i see is this one uses build in datasource..?

edit sorry wrong paste

stephen

@mikael @cvp @Appletrain sorry about post miss-hap.. i found this to work. but it bypasses dialogs module..

import ui

class DataSource(object):
    def __init__(self, items):
        self.items=items

    def tableview_cell_for_row(self, tableview, section, row):
        # Create and return a cell for the given section/row
        cell = ui.TableViewCell()
        cell.text_label.text =  self.items[row]
        return cell

    def tableview_number_of_sections(self, tableview):
        # Return the number of sections (defaults to 1)
        return 1

    def tableview_number_of_rows(self, tableview, section):
        # Return the number of rows in the section
        return len(self.items)

class TV:
    def __init__(self):
        self.menu_one_options = ['Banana', 'Orenge', 'Grape']
        self.menu_two_options = ['happy', 'days', 'are', 'here', 'again']
        frame=(0, 0, 200, 600)

        self.selected=''

        self.tv1 = ui.TableView(frame=frame)
        self.tv1.name='menu-one'
        self.tv1.delegate = self
        self.tv1.hidden=False
        self.tv1.data_source = DataSource(self.menu_one_options)
        self.tv1.present('sheets')

        self.tv2 = ui.TableView(frame=frame)
        self.tv2.name='menu-two'
        self.tv2.delegate = self
        self.tv2.hidden=True
        self.tv2.data_source = DataSource(self.menu_two_options)


    def tableview_did_select(self, tableview, section, row):
        if not self.tv1.hidden:
            self.selected += tableview.data_source.items[row] + ' '
            self.tv1.hidden=True
            self.tv2.present('sheets')
            self.tv2.hidden=False
        elif not self.tv2.hidden:
            self.selected += tableview.data_source.items[row]
            print (self.selected)
            self.tv2.hidden=True
            self.tv1.close()
            self.tv2.close()

cvp

@stephen said:

@ Appletrain

One space to be removed πŸ™„

stephen

@cvp πŸ˜…πŸ˜… oops

Appletrain

Thank you all. @ui.in_backround solved it.

cvp

@Appletrain youpee