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

Rag*_*tto 19 iphone core-data ios watchkit wkinterfacetable

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

在此输入图像描述

相同的数据也会显示在观看应用中:

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述

我看到旧行空了!

在此输入图像描述

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

在此输入图像描述

这是在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"] ]];
            });
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

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

- (IBAction)reloadTable {        
    [self loadTable];
}
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

Bon*_*nan 11

在@leolobato发布的链接中,实际上有一种解决方法.它确实解决了我的问题.

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

当你改变行时,首先将它们设置为= @"".

例如,如果你的行有一个row.Label并且你要更改它,那么在row.Label.text = @"[Actual text]"之后再次执行row.Label.text = @""

  • 这在xcode 6.2中得到修复,因此无需解决方法. (4认同)

Jay*_*Jay 5

调用setRowTypes:setNumberOfRows:withRowType:方法.

以下是开发人员文档中的声明,我相信这会起作用.

如果要更新表的内容,请使用新的行类型信息再次调用setRowTypes:或setNumberOfRows:withRowType :. 再次调用这些方法会强制表丢弃旧行并创建新行.要在不删除旧行的情况下插入新行,请使用insertRowsAtIndexes:withRowType:方法.

链接到文档