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 do not work?

    Pythonista
    2
    3
    3498
    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.
    • beer2011
      beer2011 last edited by

      Hello, all.
      This code do not work.
      When you tap , it will not work. (~~;;)
      Any suggestion,please...

      
      # coding: utf-8
      
      import ui
      import console
      
      class MyTableView(object):
      	def __init__(self):
      		foods = [{'title': 'Vegitable'}, 
      		         {'title': 'Fruits'}, 
      		         {'title': 'Fish'}]
      		self.list = ui.ListDataSource(foods)
      		
      		self.tv = ui.TableView()
      		self.tv.name = 'Kind'
      		self.tv.delegate = self.tv.data_source = self.list
      		
      		nv = ui.NavigationView(self.tv)
      		nv.name = 'Foods'
      		nv.present('sheet')
      		
      	def tableview_did_select(self, tableview, section, row):
      		self.tv.name = self.list[row]['title']
      		self.tv.delegate = self.tv.data_source = self.list
      		table_view.navigation_view.push_view(self.tv)
      
      	def tableview_number_of_sections(self, tableview):
      		return 1
      
      	def tableview_number_of_rows(self, tableview, section):
      		return len(self.list)
      
      	def tableview_cell_for_row(self, tableview, section, row):
      		cell = ui.TableViewCell()
      		cell.text_label.text = self.list
      		return cell
      
      ### main ########################
      MyTableView()
      
      
      1 Reply Last reply Reply Quote 0
      • Sebastian
        Sebastian last edited by

        It doesn't work because the tableview delegate is set to ui.ListDataSource() instead of MyTableView.
        I think you should use MyTableView as both the data source and as the delegate.
        Maybe something like this?

        # coding: utf-8
        
        import ui
        import console
        
        class MyTableView(object):
            def __init__(self):
                self.list = [{'title': 'Vegitable'}, 
                         {'title': 'Fruits'}, 
                         {'title': 'Fish'}]
        
                self.tv = ui.TableView()
                self.tv.name = 'Kind'
                self.tv.delegate = self
                self.tv.data_source = self
        
                nv = ui.NavigationView(self.tv)
                nv.name = 'Foods'
                nv.present('sheet')
        
            def tableview_did_select(self, tableview, section, row):
                tv = ui.TableView()
                tv.name = self.list[row]['title']
                tv.delegate = self
                tableview.navigation_view.push_view(tv)
        
            def tableview_number_of_sections(self, tableview):
                return 1
        
            def tableview_number_of_rows(self, tableview, section):
                return len(self.list)
        
            def tableview_cell_for_row(self, tableview, section, row):
                cell = ui.TableViewCell()
                cell.text_label.text = self.list[row]['title']
                return cell
        
        ### main ########################
        MyTableView()
        
        1 Reply Last reply Reply Quote 0
        • beer2011
          beer2011 last edited by

          @Sebastian
          Thank you! It works,well!
          And then, the sub menu was added to 'fruits'.
          There may be a better way...

          
          # coding: utf-8
          
          import ui
          import console
          
          class MyTableView(object):
          	def __init__(self):
          		self.list = [{'title': 'Vegitable'}, 
          		         {'title': 'Fruits'}, 
          		         {'title': 'Fish'}]
          
          		self.tv = ui.TableView()
          		self.tv.name = 'Kind'
          		self.tv.delegate = self
          		self.tv.data_source = self
          		
          		nv = ui.NavigationView(self.tv)
          		nv.name = 'Foods'
          		nv.present('sheet')
          		
          	def tableview_did_select(self, tableview, section, row):
          		tv = ui.TableView()
          		tv.name = self.list[row]['title']
          
          		if tv.name == 'Fruits':
          			sub_ds = SubTableView()
          			tv.data_source = sub_ds
          			tv.delegate  = sub_ds
          			tableview.navigation_view.push_view(tv)
          		else:
          			tv.delegate  = self
          			tableview.navigation_view.push_view(tv)
          
          	def tableview_number_of_sections(self, tableview):
          		return 1
          
          	def tableview_number_of_rows(self, tableview, section):
          		return len(self.list)
          
          	def tableview_cell_for_row(self, tableview, section, row):
          		cell = ui.TableViewCell()
          		cell.text_label.text = self.list[row]['title']
          		return cell
          		
          class SubTableView(object):
          	def __init__(self):
          		self.fruits = [{'title': 'Banana'},
          		                    {'title': 'Orenge'},
          		                    {'title': 'Grape'}]
          		
          		self.tv = ui.TableView()
          		self.tv.delegate = self
          		self.tv.data_source = self
          		
          	def tableview_did_select(self, tableview, section, row):
          		tv = ui.TableView()
          		tv.name = self.fruits[row]['title']
          		tv.delegate  = self
          		tableview.navigation_view.push_view(tv)
          
          	def tableview_number_of_sections(self, tableview):
          		return 1
          
          	def tableview_number_of_rows(self, tableview, section):
          		return len(self.fruits)
          
          	def tableview_cell_for_row(self, tableview, section, row):
          		cell = ui.TableViewCell()
          		cell.text_label.text = self.fruits[row]['title']
          		return cell
          		
          		
          ### main ########################
          MyTableView()
          
          
          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Powered by NodeBB Forums | Contributors