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

标签 ios iphone core-data watchkit wkinterfacetable

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

enter image description here

相同的数据也显示在 watch 应用中:

enter image description here

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

enter image description here

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

enter image description here

我看到旧行是空的!

enter image description here

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

enter image description here

这是在 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];
}

我哪里错了?

最佳答案

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

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]"

关于ios - 如何在 WatchKit tableview 中重新加载数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28031832/

相关文章:

iphone - 在 iPhone 中的 ScrollView 中交换图像

iphone - uiscrollview setcontentoffset 动画动画时不滚动的问题 :yes is set

ios - 通过 MFMailcomposeController 发送时在 sqlite 中发现空数据

ios - Swift TableView 删除单元格不显示

objective-c - ARC引用计数解除分配和释放

ios - 在剩余的数字中,我想选择一些数字,但基于某个计数器变量,其值是随机确定的

ios - 如何为 iOS App 设置默认语言设置

android - React Native - 当我在 API 调用加载时锁定屏幕时,应用程序进程停止

iphone - PerformSegueWithIdentifier 和 popViewControllerAnimated 破屏导航

iphone - 核心数据-内存使用情况: my app crashes