Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Filling tableView gradually
-
Hiya
I’m trying to fill a tableView with rows one after the other with a slight delay between each cell appearing.This is how I’m trying to do it - but they all still appear in one go:
def tableview_cell_for_row(tableview, section, row): cell =ui.TableViewCell("subtitle") data = tableview.data_source.items[row] cell.bg_color = (0,1,0,0.1) cell.text_label.font= ('system',(17*Hmult)) cell.text_label.text_color = (0,1,0,0.0) cell.text_label.text = str(data) selected_cell = ui.View() selected_cell.bg_color = (0,1,0,0.2) selected_cell.text_color = (0,1,0,1.0) cell.selected_background_view = selected_cell def anim(): if len(tableview.data_source.items) != row: print(row) cell.text_label.text_color = (0,1,0,0.5) ui.delay(anim,0.2) ui.delay(anim,0.2) return cell
-
@rb Put your delay in filling, not in display
import ui import time def tableview_cell_for_row(tableview, section, row): cell =ui.TableViewCell("subtitle") data = tableview.data_source.items[row] cell.bg_color = (0,1,0,0.1) cell.text_label.text = str(data) selected_cell = ui.View() selected_cell.bg_color = (0,1,0,0.2) selected_cell.text_color = (0,1,0,1.0) cell.selected_background_view = selected_cell return cell v = ui.View() f = (0, 0, 600, 800) v.frame = f tbl = ui.TableView() tbl.frame = f tbl.data_source = ui.ListDataSource(items=[]) tbl.data_source.tableview_cell_for_row = tableview_cell_for_row v.add_subview(tbl) v.present('sheet') for i in range(1,20): time.sleep(0.2) tbl.data_source.items.append(i)
Édit: but all rows are redisplayed when a new row is added
-
You can also use update
import ui def table_tapped(sender): rowtext = sender.items[sender.selected_row] print(rowtext) class TableUpdate(ui.View): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.reset_value = 2 self.stop_value = 20 self.value = self.reset_value self.state = 'stop' self.update_interval = 1 self.add_subview(ui.TableView(name='tableview1', frame=(0,0,400,400))) self.reset_table_view() self.add_subview(ui.Button(title='Reset', frame=(50,450,100,100), action=button_action)) self.add_subview(ui.Button(title='Start', frame=(160,450,100,100), action=button_action)) self.add_subview(ui.Button(title='Stop', frame=(270,450,100,100), action=button_action)) def reset_table_view(self): self.value = self.reset_value self.update_table_view() def update_table_view(self): self['tableview1'].data_source = ui.ListDataSource(range(self.value)) self['tableview1'].data_source.action = table_tapped self['tableview1'].delegate = self['tableview1'].data_source self['tableview1'].reload_data() def update(self): if self.state == 'run': if self.value < self.stop_value: self['tableview1'].data_source.items.append(self.value) self['tableview1'].reload_data() self.value += 1 self.set_needs_display() def button_action(sender): v1 = sender.superview if sender.title == 'Reset': v1.state = 'stop' v1.reset_table_view() elif sender.title == 'Start': v1.state = 'run' elif sender.title == 'Stop': v1.state = 'stop' v = TableUpdate(frame=(0,0,400,500), name='view1') v.present('sheet')
-
@rb If you want to vary opacity
import ui import time def tableview_cell_for_row(tableview, section, row): global t cell =ui.TableViewCell("subtitle") data = tableview.data_source.items[row]['title'] opac = tableview.data_source.items[row]['opacity'] cell.bg_color = (0,1,0,0.1) cell.text_label.text = str(data) cell.text_label.text_color = (0,1,0,opac) selected_cell = ui.View() selected_cell.bg_color = (0,1,0,0.2) selected_cell.text_color = (0,1,0,1.0) cell.selected_background_view = selected_cell return cell v = ui.View() f = (0, 0, 600, 800) v.frame = f tbl = ui.TableView() tbl.frame = f tbl.data_source = ui.ListDataSource(items=[]) for i in range(1,21): tbl.data_source.items.append({'title':i,'opacity':0.0}) tbl.data_source.tableview_cell_for_row = tableview_cell_for_row v.add_subview(tbl) v.present('sheet') sec_per_row = 1.0 nbr_opacities = 10 for row in range(0,len(tbl.data_source.items)): for t in range(1,nbr_opacities): time.sleep(sec_per_row/nbr_opacities) tbl.data_source.items[row]['opacity'] = t/nbr_opacities tbl.reload()
-
Guys, this is what
insert_rows
is for! Don't use reload_table! -
@JonB the worst is that I had read it 😢 Shame on me
-
I’m actually using the reload method and it seems fine - I guess it’s just really inefficient is that what your saying @JonB ?
Thanks for help so far everyone.