The below code should be so simple. But it's been sometime since I have done it. I feel I am am right in what I have done, but history tells me, I am normally wrong. The docs say you can set a ui.ListDataSource item as the delegate to a TableView. Which is what I have done. But I am not getting called back. Ok, I have subclassed ui.ListDataSource, but from what I can see it should work. @omz sorry to ask, but am I just doing something stupid or is it broken?
Edit: had to edit, otherwise I would be having coding nightmares of @ccc tracking me down in Pokemon and capturing me 😱😱
import ui, editor
'''
Pythonista Forum - @Phuket2
'''
def dataset(nb=12, acc_type = 'detail_button'):
# stupid helper test func.
# make a list of dicts for ui.ListDataSource
# no list[dict] comprehension. This is clearer for this purpose
lst = []
for i in range(1, nb+1):
d = dict(title = str(i),
image= 'None',
accessory_type = acc_type)
lst.append(d)
return lst
class MyData(ui.ListDataSource):
def __init__(self, items = None, *args, **kwargs):
items = items or [] # edited
super().__init__(items, *args, **kwargs)
self.items = dataset()
# as far as i can tell, given this is the delegate if should be,
# called. But its not called when a different row is selected.
# the same with other callbacks.
def action(self, sender):
print('in action')
def accessory_action(self, sender):
print('in accessory_action')
class MyClass(ui.View):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.tbl = None
self.make_view()
def make_view(self):
self.tbl = ui.TableView(name = 'table', frame = self.bounds)
self.tbl.flex = 'wh'
ds = MyData()
self.tbl.data_source = ds
self.tbl.delegate = ds
self.add_subview(self.tbl)
if __name__ == '__main__':
_use_theme = True
w, h = 300, 400
f = (0, 0, w, h)
mc = MyClass(frame=f, bg_color='white', name ='I am Broken 😱')
if not _use_theme:
mc.present('sheet', animated=False)
else:
editor.present_themed(mc, theme_name='Oceanic', style='sheet', animated=False)