@colint ok, I'll stop with this last code, surely not perfect but, I think, sufficient to show you that you can do it like you asked. Even long swipe (where delete button width is more than half screen) is foreseen and simulates the tap of the delete button. Try the code and give me some feed-back.
I'm sure that you could ameliorate it in a better way than mine,
import console
import ui
class cell_view(ui.View):
def __init__(self,w,h,tableview,row):
self.width = w
self.height = h
self.tv = tableview
self.row = row
if self.tv.swiped:
prev = self.tv.swiped
prev['bdel'].x = self.width
prev['byou'].x = prev['bdel'].x + prev['bdel'].width
self.tv.swiped = self
#self.border_width = 1
#self.border_color = 'red'
bdel = ui.Button(name='bdel')
bdel.frame = (w-0,0,w/8,self.height)
bdel.background_color = 'red'
bdel.tint_color = 'white'
bdel.title = 'delete'
bdel.action = self.but_action
self.add_subview(bdel)
byou = ui.Button(name='byou')
byou.frame = (bdel.x+bdel.width,0,w/8,self.height)
byou.background_color = 'green'
byou.tint_color = 'white'
byou.title = 'your edit'
byou.action = self.but_action
self.add_subview(byou)
def touch_began(self, touch):
#print('touch_began')
if self.tv.swiped:
prev = self.tv.swiped
if prev != self:
prev['bdel'].x = self.width
prev['byou'].x = prev['bdel'].x + prev['bdel'].width
self.tv.swiped = None
self.x0,self.y0 = touch.location
def touch_moved(self, touch):
x,y = touch.location
if abs(y-self.y0) < 100:
# swipe left or right
self['bdel'].x = min(max(self['bdel'].x + 2*(x-touch.prev_location.x), 0), self.width)
self['bdel'].width = max(self.width/8,self.width*7/8-self['bdel'].x)
#self['bdel'].x = min(max(self['bdel'].x + 2*(x-touch.prev_location.x), self.width*3/4), self.width)
self['byou'].x = self['bdel'].x + self['bdel'].width
self.tv.swiped = self
def touch_ended(self, touch):
if self['bdel'].width > self.width/2:
# automatic delete
self.but_action('simul delete button')
def but_action(self,sender):
tbl = self.tv
row = self.row
self['bdel'].x = self.width
self['bdel'].width = self.width/8
self['byou'].x = self['bdel'].x + self['bdel'].width
self.tv.swiped = None
if sender.title == 'delete' or isinstance(sender, str):
b = console.alert('delete row',str(row), 'confirm', 'cancel', hide_cancel_button=True)
if b == 1:
del tbl.data_source.items[row]
tbl.reload()
elif sender.title == 'your edit':
t = tbl.data_source.items[row]
t = console.input_alert('enter text of row', str(row), t, 'ok', hide_cancel_button=True)
tbl.data_source.items[row] = t
class MyTableView(ui.View):
def __init__(self,w,h):
self.width = w
self.height = h
tbl = ui.TableView()
tbl.swiped = None
tbl.frame = (0,0,w,h)
tbl.row_height = 50
tbl.data_source = ui.ListDataSource(items=['a','b','c','d','e'])
tbl.delegate = self
tbl.data_source.tableview_cell_for_row = self.tableview_cell_for_row
tbl.data_source.tableview_can_delete = self.tableview_can_delete
tbl.background_color = (0,0,0,0)
self.add_subview(tbl)
def tableview_cell_for_row(self, tableview, section, row):
# Create and return a cell for the given section/row
cell = ui.TableViewCell()
cell.selectable = False
cell.text_label.text = tableview.data_source.items[row]
v = cell_view(self.width,tableview.row_height,tableview,row)
cell.content_view.add_subview(v)
return cell
def tableview_can_delete(self, tableview, section, row):
# Return True if the user should be able to delete the given row.
return False # to be sure that standard delete button is not displayed
def main():
# Hide script
w,h = ui.get_screen_size()
mi = min(w,h)*0.9
my_back = MyTableView(mi,mi)
my_back.background_color='white'
my_back.name = 'Test for @colint'
my_back.present('sheet',hide_title_bar=False)
my_back.wait_modal()
# Protect against import
if __name__ == '__main__':
main()