I was just playing around. Just thought I would share what I was doing. I was trying to basically make a checkmark list. I was trying to keep it simple and small. I might look a little on the large size, its mainly the comments, there is not much to it though.
Basically its just me thinking out aloud. The dialogs package is great, but still is hard to override it's behaviour easily. I am using multiple inheritance, which some may say is bad form. I am not sure. But I put some notes above the class about what I was doing. It's not supposed to be a finished class. More like just a test before going further.
Maybe useful for new Pythonista members new to the ui module. However, as i say, some may take exception to the use of the multiple inheritance.
import ui
'''
poor mans list dialog... Just playing around. Wanted to see how the
multiple inheritance would workout. It seems to have worked ok!
I was hoping to make it a bit smaller in code lines than it is,
but the logic is the logic. Could probably shave off a few lines,
but not that many I think.
Besides playing around I was thinking of having a number of these
classes just specfic to a single pupose like a checkmark list etc...
But keeping them small and managable.
The dialogs module is good. But it is a bit inflexible. Sometimes
you have to fight hard to override some behaviour you may want/need
Anyway, this is just a start or thinking out aloud. I will make some mods,
to handle sections and ui.TableViewCell's
I also included the image in the dict but not doing anthing with it as yet.
I think its still a good example if you are new to Pythonista to see
how easily you can make your own list. HOWEVER, not sure what the seasoned
Python programmers here will think of me using multiple inheitance.
What i have done maybe be considered bad form, I am not sure.
I was trying to get a small footprint. I couldn't see any reason why this
should be bad. The base classes and there params dont overlap.
'''
class CheckMarkTable(ui.View, ui.ListDataSource):
def __init__(self, items=None, selection=None, *args, **kwargs):
# call the base init methods explicitly. I know there is a way
# to call with super and it works it out, but I have had problems
# with that past. This seems more straight fwd to me.
ui.View.__init__(self, *args, **kwargs)
ui.ListDataSource.__init__(self, [])
# setup up the ui.TableView, pointing data_source and delegate to self
tbl = ui.TableView(frame=self.bounds, flex='WH')
tbl.data_source = tbl.delegate = self
self.action = self._action
items = items or []
self.items = [{'title': str(item), 'image': None, 'accessory_type': ''}
for item in items]
self.selected = selection
self.set_checkmark(selection)
self.add_subview(tbl)
def _action(self, sender):
# called when a cell is tapped
self.set_checkmark(sender.selected_row)
def set_checkmark(self, row):
if row is None:
return
# if the same row is selected, toggle the value
if self.selected == row:
if self.items[row]['accessory_type'] == 'checkmark':
self.items[row]['accessory_type'] = ''
else:
self.items[row]['accessory_type'] = 'checkmark'
else:
# clear the previous selections and set the new selection
self.clear_selection()
self.items[row]['accessory_type'] = 'checkmark'
self.selected = row
self.reload()
def get_selected_item(self):
# get the data item of the selected row
if self.selected is not None:
return self.items[self.selected]
def clear_selection(self):
# set all the item['accessory_type'] = ''
for item in self.items:
item['accessory_type'] = ''
if __name__ == '__main__':
f = (0, 0, 320, 480)
items = ['Red', 'Green', 'Blue', 'Black', 'Orange', 'Yellow']
t = CheckMarkTable(frame=f, items=items, selection=None, name='List')
t.present('sheet', animated=False)
t.wait_modal()
print(t.get_selected_item())