2015-11-17 31 views
1

我有一個已發佈的應用程序,現在我想要構建一個手錶擴展,它只會在手錶上顯示填充了來自我的核心數據存儲的數據的表。現在非常簡單。所以我基本上試圖使用我的應用程序tableviewcontroller修改爲手錶擴展的代碼。我擁有應用程序組,並創建了應用程序和手錶擴展可以訪問的核心數據類。儘管在基於調試日誌的手錶擴展中這似乎工作正常,但我在手錶上運行應用程序時,手錶中沒有任何數據。代碼和調試日誌都低於:WatchKit,核心數據和表格

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 

回答

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

在很多雨燕

更優雅
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") 
} 
+0

當我用上面的代碼仍然沒有看到任何數據。爲了確保數據在覈心數據存儲中,我添加了一些NSLog調用來指示是否從數據源(Web服務調用)創建並存儲在覈心數據存儲中的任何數據,並且日誌顯示數據正在儲存。但是,結果數組數顯示爲0,所以表代碼可能正常工作,這是產生結果的代碼是問題。我會繼續嘗試找到問題。 – bachma0507

+0

我發現問題的結果。我修好後,這工作。謝謝! – bachma0507