ios - WatchKit、核心数据和表格

标签 ios core-data watchkit watchos-2 wkinterfacetable

我有一个已发布的应用程序,现在我想构建一个 watch 扩展程序,它只会在 watch 上显示一个表格,其中填充了来 self 的核心数据存储的数据。现在非常简单。所以我基本上尝试使用我在我的应用程序 tableviewcontroller 中为 watch 扩展修改的代码。我有应用程序组,并创建了一个应用程序和 watch 扩展程序都可以访问的核心数据类。尽管这在基于调试日志的 watch 扩展中似乎工作正常,但当我在 watch 上运行该应用程序时,我没有在 watch 表中获取任何数据。代码和调试日志如下:

接口(interface) Controller .h

#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
#import "CoreDataHelper.h"

@interface InterfaceController : WKInterfaceController

@property (strong, nonatomic) IBOutlet WKInterfaceTable *table;
@property (strong, nonatomic) NSArray *objects;

@end

接口(interface) Controller .m:

#import "InterfaceController.h"
#import "ScheduleTableRow.h"

@interface InterfaceController()

@end

@implementation InterfaceController

@synthesize objects; 

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

// Configure interface objects here.

NSManagedObjectContext * managedContext = [[CoreDataHelper sharedHelper] context];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Cschedule" inManagedObjectContext:managedContext];

NSManagedObject *object = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:managedContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

[fetchRequest setEntity:entity];

NSArray *myResults = [managedContext executeFetchRequest:fetchRequest error:nil];

self.objects = myResults;

NSLog(@"Results count: %lu", (unsigned long)myResults.count);

[self.table setNumberOfRows:myResults.count withRowType:@"ScheduleTableRow"];

for (object in myResults) {

    ScheduleTableRow *scheduleRow = [self.table rowControllerAtIndex:[myResults indexOfObject:object]];
    [scheduleRow.name setText:[NSString stringWithFormat:@"%@",[object valueForKey:@"day"]]];
    [scheduleRow.date setText:[NSString stringWithFormat:@"%@",[object valueForKey:@"date"]]];
}

}

- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
[super willActivate];
}

- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
[super didDeactivate];
}

@end

调试日志:

2015-11-17 15:07:12.143 canada2016Watch Extension[89456:1406057] Running CoreDataHelper 'init'
2015-11-17 15:07:12.148 canada2016Watch Extension[89456:1406057] Running CoreDataHelper 'setupCoreData'
2015-11-17 15:07:12.148 canada2016Watch Extension[89456:1406057] Running CoreDataHelper 'loadStore'
2015-11-17 15:07:12.148 canada2016Watch Extension[89456:1406057] Running CoreDataHelper 'storeURL'
2015-11-17 15:07:12.148 canada2016Watch Extension[89456:1406057] Running CoreDataHelper 'applicationStoresDirectory'
2015-11-17 15:07:12.148 canada2016Watch Extension[89456:1406057] Running CoreDataHelper 'applicationDocumentsDirectory'
2015-11-17 15:07:12.159 canada2016Watch Extension[89456:1406057]    Successfully added store: <NSSQLCore: 0x7b8692b0> (URL:   file:///Users/barry/Library/Developer/CoreSimulator/Devices/E7C26A87-B22C-45C5-9AE3-18CB24393EA9/data/Containers/Data/PluginKitPlugin/6717F002-7F1B-44EB-8C9F-861B6CF01E08/Documents/Stores/group.org.bicsi.canada2016app)
2015-11-17 15:07:12.160 canada2016Watch Extension[89456:1406057] Results count: 1

最佳答案

也许您误解了 rowControllerAtIndex 方法。此方法是您向表中添加行的实际位置。相反,您将索引引用为 indexOfObject,尽管该表还没有任何对象。

相反,您应该遍历索引,为每一行创建一个项目并用适当的对象填充它。

for (int i = 0; i < self.objects.count; i++) {
   ScheduleTableRow *scheduleRow = [self.table rowControllerAtIndex:i];
   NSManagedObject *item = self.objects[i];
   scheduleRow.name.text = [item valueForKey:@"day"];
   scheduleRow.date.text = [item valueForKey:@"date"];
}

在 Swift 中更优雅

for (idx, object) in self.objects.enumerate() {
   let scheduleRow = self.table.rowControllerAtIndex(idx)
   scheduleRow.name.text = object.valueForKey("day")
   scheduleRow.date.text = object.valueForKey("date")
}

关于ios - WatchKit、核心数据和表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33766429/

相关文章:

ios - 将 watch 应用程序添加到 Flutter iOS 应用程序

ios - "ViewController.Type does not have a member named ' 变量

ios - MPMusicPlayer 设置

android - 我可以在 Android 或基于 Web 的应用程序上使用 CloudKit 吗?

objective-c - 核心数据 NSManagedObject : Max Number of Attributes?

ios - Xcode 6.2 beta 4 是否让 watchKit 接口(interface)与页面无法打开推送接口(interface)?

ios - Restkit 0.20 CoreData error "Can' t find model for source store”

ios - 核心数据 : Set Managed Object Context from AppDelegate for app with UITabBarController

cocoa - 匹配核心数据存储中的近似字符串

ios - 与 WatchOS 共享 Realm 数据