Forum Archive

Refreshing/Replacing Tableview Data

cook

I'm wondering if anyone has a suggestion for how to refresh the data for a tableview and for it to display!

I have a UI with a search bar, and the resulted list is displayed in a tableview. The textfield (search bar) action just runs the function to get new data, then run through my datasource class for the tableview. I know that everything works as I've tested it without the search bar action, but that is I believe where the problem is.

I've tried tableview.refresh() and tableview.refresh_data() in my action function to no avail.

I've never tried to do something like this, so I'm not sure exactly how it should work. I know how to get initial data displayed in a tableview, but replacing it entirely with different data via a function ....?

Thanks guys always for your help!

Phuket2

@cook , just guessing but I think you need to use TableView.reload or TableView.reload_data

cook

Whoops sorry in my post above I meant to have reload/reload_data

shaun-h

@Phuket2 is right reload_data should do it. I use it in a few scripts I've written can you post the code you are trying to do it?

shaun-h

I have actually created you a very poor example of reloading data, click the add and type some text to try it out

# coding: utf-8
import ui
import dialogs

class tv (object):
    def __init__(self):
        self.data = []

    def tableview_did_select(self, tableview, section, row):
        pass

    def tableview_number_of_sections(self, tableview):
        return 1

    def tableview_number_of_rows(self, tableview, section):
        return len(self.data)

    def tableview_cell_for_row(self, tableview, section, row):
        cell = ui.TableViewCell()
        cell.text_label.text = self.data[row]
        return cell

data = tv()
v = ui.TableView()
def add(sender):
    data.data.append(dialogs.input_alert(title='Please Enter'))
    v.reload_data()
if __name__ == '__main__':

    v.right_button_items = [ui.ButtonItem(title='Add',action=add)]
    v.data_source = data
    v.delegate = data
    v.present()
cook

@shaun-h , @Phuket2 thanks for your help... I have it working now. It was a silly mistake in how I changed the data.

I saw clearly from Shaun-h's example that I was changing the data in the class wrong.

I was doing something like data_source = Tv(data) rather than data_source.data = data

Much obliged!

Phuket2

@cook , look for something very simple. Normally it just works for me. But I have also made simple mistakes. When I look for a complicated explanation, I often miss a mis spelling etc, that passes as being valid