如何在 WatchKit tableview 中重新加载数据?

How can I reload the data in a WatchKit tableview?

我有一个 iPhone 的应用程序,它有一个 Tableview,其数据来自 CoreData。

手表应用程序中也显示相同的数据:

如果我从 iPhone 应用添加一行:

然后我在 Watch 应用程序中重新加载数据:

我看到旧行是空的!

如果我停止手表应用程序并再次启动它,一切都会正确显示!

这是在手表应用中填充 Tableview 的代码

-(void)awakeWithContext:(id)context{
    [super awakeWithContext:context];
       [self loadTable];
}

-(void)loadTable{
    NSLog(@"loadTableData");
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Data"];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Data"
        inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sortByDate = [[NSSortDescriptor alloc] initWithKey:@"sortedDateAndTime" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByDate, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    self.watchMArray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

  //  [self.watchTableView setRowTypes:self.watchMArray];
    [self.watchTableView setNumberOfRows:self.watchMArray.count withRowType:@"data"];

    for (NSInteger i = 0; i < self.watchMArray.count; i++)
    {
        WatchTableCell *cell = [self.watchTableView rowControllerAtIndex:i];
        NSManagedObject *data = [self.watchMArray objectAtIndex:i];
        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
            //Background Thread
            UIImage *foto =[self loadImageFromData:[data valueForKey:@"imageData"]];
            dispatch_async(dispatch_get_main_queue(), ^(void){
                //Run UI Updates
            [cell.watchImage setImage:foto];
            [cell.watchDate setText:[NSString stringWithFormat:@"%@", [data valueForKey:@"dataEOra"] ]];
            });
        });
    }
}

这是我目前用来重新加载它的代码:

- (IBAction)reloadTable {        
    [self loadTable];
}

我哪里错了?

正在调用 setRowTypes:setNumberOfRows:withRowType: 方法。

以下是开发者文档中的声明,我相信这会奏效。

When you want to update the contents of a table, call setRowTypes: or setNumberOfRows:withRowType: again with the new row type information. Calling these methods again forces the table to discard the old rows and create new ones. To insert new rows without removing the old ones, use the insertRowsAtIndexes:withRowType: method.

Link to document

要重新加载数据,请将 IBAction 更改为:

- (IBAction)reloadTable {
    [self.watchTableView setNumberOfRows:self.watchMArray.count withRowType:@"data"];
}

来自Apple's developer documentation on WKInterfaceTable

This method removes any existing rows from the table and configures a new set of rows based on the information in the numberOfRows and rowType parameters.

鉴于您在原始代码中使用了 setNumberOfRows:withRowType:,我在这里使用了它。方法setRowTypes:也可以达到同样的效果。

在@leolobato 发布的 link 中,实际上有一个解决方法。它确实解决了我的问题。

https://devforums.apple.com/message/1098875#1098875

when you change your rows just set them to =@"" first.

For example, if your row has a row.Label and you are changing it, do row.Label.text = @"" then again right after row.Label.text =@"[Actual text]"

我发现调用 setNumberOfRows:rowType 不会刷新 table,除非您从 willActivate 方法调用它。我有一个从模态控制器调用的委托方法,它会从父应用程序获取一组新记录,然后重新加载 table。尽管打印出来的 numberOfRows 显示了我设置的新行数,但在视觉上现有的行仍然显示在屏幕上。只是在我的委托方法中设置了一个标志,然后在 willActivate 中检查了该标志,然后才重新加载 table,它是否刷新了显示。

我相信你必须同时做两件事才能获得 tableview.reloadData() iOS 函数的相同结果,所以要在手表中重新加载 table数据执行以下操作: 1 次调用 table 方法:setNumberOfRows 2 遍历刷新值的行。

vacationList.setNumberOfRows(dict.count, withRowType: "vacationRow")
    for index in 0..<vacationList.numberOfRows {
        if let controller = vacationList.rowControllerAtIndex(index) as? VacationRowcontroller {
            controller.vacation = dict.objectForKey("\(index)") as? NSDictionary
        }
    } 

提示:dict = 从后端重新加载的新数据的字典。

对于 watchOS 5.1,iOS 12.1 只有当我在 didAppear 方法中添加 setNumberOfRows:withRowType: 时它才有效。 使用 awake(withContext:)willActivate 对我不起作用。前 4 行是空白的。