text: # coding: utf-8
  from operator import itemgetter
  
  import datetime
  import os
  import ui
  
  
  class MyTableViewDataSource(object):
      def __init__(self, row_height):
          self.row_height = row_height
          self.width = None
  
      def tableview_number_of_rows(self, tableview, section):
          return len(tableview.data_source.items)
  
      def tableview_cell_for_row(self, tableview, section, row):
          self.width, height = ui.get_screen_size()
          cell = ui.TableViewCell()
          cell.bounds = (0, 0, self.width, self.row_height)
          for i in range(3):
              self.make_labels(cell, tableview.data_source.items[row][i], i)
          return cell
  
      def make_labels(self, cell, text, pos):
          label = ui.Label()
          label.border_color = "lightgrey"
          label.border_width = 0.5
          if pos == 2:
              label.text = str(datetime.datetime.fromtimestamp(text))
          else:
              label.text = str(text)
          label.frame = (pos * self.width / 3, 0, self.width / 3, self.row_height)
          label.alignment = ui.ALIGN_CENTER
          cell.content_view.add_subview(label)
  
  
  class MyTableViewDelegate(object):
      def tableview_did_select(self, tableview, section, row):
          print("select")
  
      def tableview_did_deselect(self, tableview, section, row):
          print("deselect")
  
  def mysorted(mylist, myindex, reverse=False):
      return sorted([elem for elem in mylist if not isinstance(elem[myindex], str)],
             key=itemgetter(myindex), reverse=reverse) + sorted(
          [elem for elem in mylist if isinstance(elem[myindex], str)],
          key=itemgetter(myindex), reverse=reverse)
  
  
  class MyTableView(ui.View):
      def __init__(self):
          self.dirs = []
          self.files = []
          self.select_color = "lightgrey"
          self.unselect_color = "white"
          self.active_button = None
          self.button_height = 50
          self.btn_name = self.make_buttons("Name")
          self.btn_size = self.make_buttons("Size")
          self.btn_date = self.make_buttons("Date")
          self.tv = ui.TableView()
          self.tv.row_height = 30
          self.tv.data_source = MyTableViewDataSource(self.tv.row_height)
          self.get_dir()
          self.all_items = self.dirs + self.files
          self.tv.data_source.items = self.all_items
          self.name = "TableView-Test"
          # self.tv.delegate = MyTableViewDelegate()
          self.tv.allows_selection = False
          self.add_subview(self.tv)
          self.present("full_screen")
  
      def make_buttons(self, name):
          button = ui.Button()
          button.name = name
          button.title = name
          button.border_color = "blue"
          button.border_width = 1
          button.corner_radius = 3
          button.background_color = self.unselect_color
          button.action = self.btn_action
          self.add_subview(button)
          return button
  
      def btn_action(self, sender):
          names = [self.btn_name.name, self.btn_size.name, self.btn_date.name]
          sender_index = names.index(sender.name)
          if sender.background_color == (
                  1.0,
                  1.0,
                  1.0,
                  1.0,
          ):  # thrid click on the same column doesn't work if it's no hardcoded color
              if sender.background_color == self.unselect_color:
                  sender.background_color = self.select_color
                  self.all_items = mysorted(self.all_items, sender_index)
              else:
                  sender.background_color = self.unselect_color
                  self.all_items = mysorted(self.all_items, sender_index, reverse=True)
          else:
              if self.active_button == None:
                  self.active_button = sender.name
              if sender.name == self.btn_name.name:
                  self.btn_name.background_color = self.select_color
                  self.all_items = mysorted(self.all_items, sender_index)
              else:
                  self.btn_name.background_color = self.unselect_color
              if sender.name == self.btn_size.name:
                  self.btn_size.background_color = self.select_color
                  self.all_items = mysorted(self.all_items, sender_index)
              else:
                  self.btn_size.background_color = self.unselect_color
              if sender.name == self.btn_date.name:
                  self.btn_date.background_color = self.select_color
                  self.all_items = mysorted(self.all_items, sender_index)
              else:
                  self.btn_date.background_color = self.unselect_color
          self.tv.data_source.items = self.all_items
          self.tv.reload()
          self.active_button = sender.name
  
      def layout(self):
          self.tv.reload()
          self.btn_name.frame = (
              0 * self.width / 3,
              0,
              self.width / 3,
              self.button_height,
          )
          self.btn_size.frame = (
              1 * self.width / 3,
              0,
              self.width / 3,
              self.button_height,
          )
          self.btn_date.frame = (
              2 * self.width / 3,
              0,
              self.width / 3,
              self.button_height,
          )
          self.tv.frame = (
              0,
              self.button_height,
              self.width,
              self.height - self.button_height,
          )
  
      def get_dir(self):
          path = os.getcwd()
          self.dirs = [] if path == os.path.expanduser("~") else [["..", "<DIR>", 0.0]]
          self.files = []
          for entry in sorted(os.listdir(path)):
              full_pathname = path + "/" + entry
              if os.path.isdir(full_pathname):
                  date = os.path.getmtime(full_pathname)
                  self.dirs.append((entry, "<DIR>", date))
              else:
                  size = os.path.getsize(full_pathname)
                  date = os.path.getmtime(full_pathname)
                  self.files.append((entry, size, date))
  
  
  if __name__ == '__main__':
      MyTableView()
  