Forum Archive

TableView reload / insert_rows

DavinE

Hey Guys,

I need help with my Issue..
My tableview works fine for me but only with the reload function....
i don't get it to work with the insert_rows function....

i Hope anyone can help me ;)
and sry for my bad English :(

Thats my Code:

import ui

def test1(tableview, section, row):
    print(tableview.data_source.items)
    tableview.data_source.items[row][1] = '+110'
    print(tableview.data_source.items)
    tableview.reload_data()
    #tableview.insert_rows([row])
    print('TEST')
    print(tableview.data_source.items)

def tableview_number_of_rows(tableview, section):
        print(len(tableview.data_source.items))
        return len(tableview.data_source.items)

def tableview_cell_for_row(tableview, section, row):
    cell =ui.TableViewCell("subtitle")
    data_textLabel = tableview.data_source.items[row][0]
    data_detailTextLable = tableview.data_source.items[row][1]
    cell.bg_color = '#98FB98' if data_detailTextLable[0] == '+' else '#F08080'
    cell.accessory_type = 'detail_button'
    cell.selectable = False

    cell.text_label.text = str(data_textLabel)
    cell.detail_text_label.text = str(data_detailTextLable)

    try:
        # iPhone 24 iPad 32
        cell.image_view.image = ui.Image.named('iob:clipboard_24')
    except AttributeError:
        pass
    return cell

v = ui.View()
f = (0, 0, 600, 800)
v.frame = f
tbl = ui.TableView()
tbl.frame = f
datasource = [['0002', '+9'], ['0001', '+18'], ['0002', '-1']]
tbl.data_source = ui.ListDataSource(items=datasource)

tbl.data_source.tableview_cell_for_row = tableview_cell_for_row
tbl.data_source.tableview_number_of_rows = tableview_number_of_rows
tbl.delegate = tbl.data_source.tableview_cell_for_row
tbl.delegate.tableview_accessory_button_tapped = test1
v.add_subview(tbl)
v.present('sheet')

print(tbl.data_source.items)
JonB

You did not insert a row, you modified the data. Insert_row only works for adding a new row, not modifying an existing one.
I suppose you could delete, then reinsert.

DavinE

Like That:

def test1(tableview, section, row): print(tableview.data_source.items) tabelview.delete_rows([row]) tableview.data_source.items[row] = ['0003', '+110'] print(tableview.data_source.items) #tableview.reload_data() tableview.insert_rows([row]) print('TEST') print(tableview.data_source.items)

I Need to check it at Home...
But i think you mean it like That ?

Or did i use .items.append ?

DavinE

i Found my Solution... but i don't know is it okay to do that so ?

def test1(tableview, section, row):
    del tableview.data_source.items[row]
    tableview.delete_rows([row])
    tableview.data_source.items.append(['0002', '+110'])
    row = len(tableview.data_source.items) - 1
    tableview.insert_rows([row])

this row:

row = len(tableview.data_source.items) - 1

need i because .append set the new data at the end of the file and without that insert_row takes another data entry "double"....

what do you mean @JonB