Thanks, I thought TableViewCells would always exist, nevermind if theyβre on screen or not. That explains why this example doesnβt work properly. Iβm going to try the suggestion of @JonB
import ui
class MyTableViewDataSource (object):
def __init__(self, data, **kwargs):
#self.__dict__.update((k, v) for k, v in kwargs.items() if k in allowed_keys)
self.__dict__.update(kwargs)
self.items = data
self.previousCell = False
self.currentCell = False
self.cells = [[] for i in range(len(self.items))]
def tableview_number_of_sections(self, tableview):
return len(self.items)
def tableview_number_of_rows(self, tableview, section):
return len(self.items[section])
def tableview_cell_for_row(self, tableview, section, row):
data = tableview.data_source.items[section][row]
cell = ui.TableViewCell('subtitle')
cell.section, cell.row = section, row
cell.text_label.text = data
self.cells[section].append(cell)
return cell
def tableview_title_for_header(self, tableview, section):
# Return a title for the given section.
# If this is not implemented, no section headers will be shown.
return 'section: '+str(section)
def tableview_can_delete(self, tableview, section, row):
# Return True if the user should be able to delete the given row.
return False
def tableview_can_move(self, tableview, section, row):
# Return True if a reordering control should be shown for the given row (in editing mode).
return False
def tableview_delete(self, tableview, section, row):
# Called when the user confirms deletion of the given row.
pass
def tableview_move_row(self, tableview, from_section, from_row, to_section, to_row):
# Called when the user moves a row with the reordering control (in editing mode).
pass
#βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class BasicTableViewDelegate(object):
def tableview_did_select(self, tableview, section, row):
# Called when a row was selected.
if tableview.data_source.currentCell:
if tableview.data_source.previousCell:
tableview.data_source.previousCell.background_color = 'white'
tableview.data_source.previousCell = tableview.data_source.currentCell
del tableview.data_source.currentCell
tableview.data_source.previousCell.background_color = '#eeeeee'
#find current cell
for cell in tableview.data_source.cells[section]: #dont like having to cycle through all cells
if cell.row == row:
tableview.data_source.currentCell = cell
break
def tableview_did_deselect(self, tableview, section, row):
# Called when a row was de-selected (in multiple selection mode).
pass
def tableview_title_for_delete_button(self, tableview, section, row):
# Return the title for the 'swipe-to-***' button.
return 'Delete'
if __name__ == '__main__':
section1 = [str(i) for i in range(10)]
section2 = [str(i) for i in range(10)]
tv = ui.TableView()
tv.data_source = MyTableViewDataSource([section1, section2])
tv.delegate = BasicTableViewDelegate()
tv.frame = (0, 0, 300, 400)
tv.present('sheet')