I really do have a brain freeze on this issue. For the life of me I can't remember how to recover a reference to a ui.TableViewCell lets say when the row is selected. I did search the forum but i could not see an answer. In the below code, I create the cells up front. But I have a feeling this is bad. I remember omz does some tricky things with tablecells so they can be re-used to save memory.
In my example below, I just want to toggle a checkmark on the selected row. I don't want to use ui.ListDataSource, because this is just the simple version I want to do.
Sorry in advance because I think the answer is going to be very simple.
Any help appreciated
import ui
class MyDataSource(object):
def __init__(self, data):
'''
I cant remember how to get access to a ui.TableViewCell, so i created
the cells and store them in a list. I have a feeling this is bad.
There must be a simple way to get the cell by index. Brain Hurts!
'''
self.data = data
self.sel_item = 0
self.cells = [ui.TableViewCell()
for _ in range(len(self.data))]
def tableview_number_of_rows(self, tableview, section):
# Return the number of rows in the section
return len(self.data)
def tableview_cell_for_row(self, tableview, section, row):
# Create and return a cell for the given section/row
cell = self.cells[row]
cell.text_label.text = self.data[row]
return cell
def tableview_did_select(self, tableview, section, row):
# Called when a row was selected.
self.select_row(row)
pass
def tableview_did_deselect(self, tableview, section, row):
# Called when a row was de-selected (in multiple selection mode).
pass
def select_row(self, sel_row):
for cell in self.cells:
cell.accessory_type = ""
self.cells[sel_row].accessory_type = 'checkmark'
self.sel_item = sel_row
def get_table(items):
tbl = ui.TableView(frame=(0, 0, 300, 400))
tbl.data_source = MyDataSource(items)
tbl.delegate = tbl.data_source
return tbl
if __name__ == '__main__':
v = get_table(['Ian', 'Fred', 'John', 'Paul'])
v.present(style='sheet', animated=False)