omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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.selected_row ambiguity?

    Pythonista
    3
    10
    1187
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • peiriannydd
      peiriannydd last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        Are you reading this from within a tableview callback? Share some code?

        1 Reply Last reply Reply Quote 0
        • peiriannydd
          peiriannydd last edited by ccc

          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')
          
          1 Reply Last reply Reply Quote 0
          • peiriannydd
            peiriannydd last edited by

            Sorry, I lost the indent tabs on the def: above

            cvp 1 Reply Last reply Reply Quote 0
            • JonB
              JonB last edited by JonB

              What if you call from within tableview_did_select? Or, add @on_main_thread before your button action?

              cvp 1 Reply Last reply Reply Quote 0
              • cvp
                cvp @JonB last edited by cvp

                @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)

                1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @peiriannydd last edited by cvp

                  @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')
                  
                  1 Reply Last reply Reply Quote 0
                  • peiriannydd
                    peiriannydd last edited by

                    Now instead of reading (0,0) when no rows are selected I read (0,<last_selected_row>) when no rows are selected

                    cvp 1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp @peiriannydd last edited by cvp

                      @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)
                      
                      1 Reply Last reply Reply Quote 0
                      • peiriannydd
                        peiriannydd last edited by

                        Thank you cvp, I appreciate your help

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors