If I read tableview.selected_row, it returns (0,0) if the first row is selected, but it also returns (0,0) if no rows are selected. How can I tell whether the first row of a tableview is selected or not?
Forum Archive
tableview.selected_row ambiguity?
Are you reading this from within a tableview callback? Share some code?
If you press the ‘check’ button when no rows are selected, it shows (0,0), the same as when the first row is selected:
import ui
def check_selected_row(sender):
#print(tbl.selected_row)
lbl.text = str(tbl.selected_row)
tbl.reload_data()
v = ui.View(bg_color='white')
tbl = ui.TableView(frame=(50,0,100,180))
data = ('zero','one','two')
tbl.data_source=ui.ListDataSource(data)
btn = ui.Button(frame=(250,100,0,0),bg_color='yellow',title='check',action=check_selected_row)
lbl = ui.Label(frame=(50,220,100,100))
v.add_subview(btn)
v.add_subview(tbl)
v.add_subview(lbl)
v.present('fullscreen')
Sorry, I lost the indent tabs on the def: above
What if you call from within tableview_did_select? Or, add @on_main_thread before your button action?
@JonB said
add @on_main_thread before your button action?
No change
Remark that if you print tbl.selected_row after present, it is already (0,0)
@peiriannydd try this code and see use of your own variable tbl.selected
import ui
def check_selected_row(sender):
#print(tbl.selected_row)
lbl.text = str(tbl.selected)
tbl.reload_data()
class TableViewDelegate():
def tableview_did_select(tableview, section, row):
tableview.selected = (section, row)
v = ui.View(bg_color='white')
tbl = ui.TableView(frame=(50,0,100,180))
tbl.delegate = TableViewDelegate
tbl.selected = (-1,-1)
data = ('zero','one','two')
tbl.data_source=ui.ListDataSource(data)
btn = ui.Button(frame=(250,100,0,0),bg_color='yellow',title='check',action=check_selected_row)
lbl = ui.Label(frame=(50,220,100,100))
v.add_subview(btn)
v.add_subview(tbl)
v.add_subview(lbl)
v.present('fullscreen')
Now instead of reading (0,0) when no rows are selected I read (0,
@peiriannydd you're right, when you reload_data, reset also tbl.selected
def check_selected_row(sender):
#print(tbl.selected_row)
lbl.text = str(tbl.selected)
tbl.reload_data()
tbl.selected = (-1,-1)
Thank you cvp, I appreciate your help