WatchKit、核心数据和表格

WatchKit, Core Data and tables

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

InterfaceController.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

InterfaceController.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方法。此方法是您向 table 添加行的实际位置。相反,您将索引引用为 indexOfObject,尽管 table 还没有任何对象。

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

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")
}