Reload table in WatchKit

We have a hierarchical application for viewing.

The root controller is a table of menu items. This list of items is controlled by the server. Data is retrieved and stored in the master data. The menu is filled when you first enter the application.

But I want this table to remain current. My thought was to add code in willActivateto check if there were changes, and reload the table. In my reload logic, I call the same function that I called for the first time, which sets menuTable.setNumberOfRowsand creates each line. Looking at what I put in the logs, it goes through this logic with a different number of lines and new shortcuts. But the application on the clock shows a table with old data.

How can I make this table reload new information?

+4
source share
4 answers

This is a bug in WatchKit. It seems that Apple is not handling the repeating interface object correctly.

The general principle here is: only insert or delete the necessary rows after creating the table. Do not reload the entire table, like what we usually do in iOS. It just does not work (or causes an error).

So you should:

  • Do it in a method willActivated. It is right.
  • If this is the first load before the table is created, do what you are doing now - download all the rows of the table.
  • For all subsequent periods, do not reload the table, retrieve new data and do not check the required number of rows.
  • , .
  • . , .

, .

+1

, , rmp, ​​watchOS 1.0.1. , tableView willActivate(), .

tableView , , . NSIndexSet .

if isNecessary {
    self.table.removeRowsAtIndexes(NSIndexSet(indexesInRange: NSMakeRange(0, maxItems)))
    isNecessary = false
}

, :

  • table.setNumberOfRows(0, withRowType: "data")

, , View .

+1

I found what works best, given the current state of watchkit, to delete all rows and then refill the table.

Try something like this:

- (void)loadTableData{

    //clear the table
    [mainTableView setNumberOfRows:0 withRowType:@"myTableRow"];

    //set the row count again
    [mainTableView setNumberOfRows:numberOfRows withRowType:@"myTableRow"];

    //populate table

}
0
source

It is pretty simple. Because Apple did not provide any way to reload the data. You can still achieve this by simply filling in the rows to represent the table.

The following is sample code:

        for index in 0..<tracksTableView.numberOfRows {
        if let controller = tracksTableView.rowController(at: index) as? EditPlaylistRowController {
            controller.playingTrackId = self.playingTrackID
            controller.sharedTrack = trackItems[index]
        }
    }

Use it when you want to update data.

Note: You can make some conditional statements inside the row controller class.

Happy to help :)

0
source

All Articles