0

正常に動作するウォッチキットテーブルをセットアップしました。しかし、コンテキストに存在するはずの値で WKInterfaceTable didSelectRowAtIndex メソッドを使用しようとすると、コンテキストは null 値を返します。私が作成したテスト コンテキスト値は正常に動作し、メソッドは動作して、正しく設定された DetailInterfaceController をプッシュします。以下の InterfaceController.m のコードを参照してください。

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

// Configure interface objects here.

    [self updateWatchTable];

}


-(void)updateWatchTable{

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

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Cschedule" inManagedObjectContext:[[CoreDataHelper sharedHelper] context]];

[fetchRequest setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"trueDate" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

NSArray *myResults = [[[CoreDataHelper sharedHelper] context] executeFetchRequest:fetchRequest error:nil];

self.objectsTable = myResults;

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

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

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

    }

}


- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex
{
// Push detail view with selected quote

NSString * scheduleDate = [[self.objectsTable objectAtIndex:rowIndex] valueForKey:@"date"];
NSString * test = @"Feb 1 2016"; //TEST date 

[self pushControllerWithName:@"DetailScheduleInterfaceController" context:test]; //THIS WORKS WITH TEST date AND CORRECTLY PUSHES POPULATED DetailInterfaceController
//[self pushControllerWithName:@"DetailScheduleInterfaceController" context:scheduleDate]; //THIS PUSHES BLANK DetailInterfaceController SINCE scheduleDate IS NULL

NSLog(@"Count in objectsTable = %lu", (unsigned long)self.objectsTable.count);
NSLog(@"Value for scheduleDate = %@", scheduleDate); //THIS GIVES NULL VALUE
}
4

2 に答える 2

0

あなたの問題は、文字列を NSManagedObject に割り当てていることだと思います。のループで行ったように実行してみてくださいupdateWatchTable

あなたのdidSelectRowAtIndex方法では:

NSManagedObject *item = self.objectsTable objectAtIndex[rowIndex];
NSString * scheduleDate = [NSString stringWithFormat:@"%@",[item valueForKey:@"date"]];
于 2015-11-19T20:11:46.013 に答える
0

以下のコードは、私にとってそれを解決したものです。for ループに MSManageObjects を格納するための配列を作成しました。次に、WKInterfaceTable didSelectRowAtIndex メソッドで、配列に対して objectAtIndex: rowIndex を実行しました。

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

self.myNewArray = [[NSMutableArray alloc] init];

    for (int i = 0; i < self.objectsTable.count; i++) {
        ScheduleTableRow *scheduleRow = [self.table rowControllerAtIndex:i];

        NSManagedObject *item = self.objectsTable[i];
        scheduleRow.name.text = [NSString stringWithFormat:@"%@",[item valueForKey:@"day"]];
        scheduleRow.date.text = [NSString stringWithFormat:@"%@",[item valueForKey:@"date"]];
        [self.myNewArray addObject:[item valueForKey:@"date"]];
    }

- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex
{
// Push detail view with selected quote

NSString * scheduleDate = [NSString stringWithFormat:@"%@", [self.myNewArray objectAtIndex:rowIndex]];

[self pushControllerWithName:@"DetailScheduleInterfaceController" context:scheduleDate];
}
于 2015-11-22T16:13:31.347 に答える