0

I setup a watchkit table which works fine. But when I try to use the WKInterfaceTable didSelectRowAtIndex method with a value that should exist for the context, the context gives a null value. A test context value I created works fine and the method works and pushes the correctly populated DetailInterfaceController. See the code below for 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
}
bachma0507
  • 1,543
  • 2
  • 11
  • 22
  • I'm assuming self.objectsTable is an array. So when you NSLog self.objectsTable in didSelectRowAtIndex what do you get? The debug method I suggest you do is NSLog self.objectsTable in didSelectRowAtIndex, if that gives you the correct result, NSLog [self.objectsTable ObjectAtIndex:rowIndex], and show us what the results are for those two things. Lets just make sure this array is populated and has the right results and a key-value pair with the key "date" – MSU_Bulldog Nov 19 '15 at 19:52
  • when I do a count I get 5, which is the correct count. Count in objectsTable = 5 – bachma0507 Nov 19 '15 at 19:56
  • Alright, so there is clearly 5 objects in your array. What about when you NSLog the objectAtIndex? Does it give you the correct result with a key-value pair with the key "date"? – MSU_Bulldog Nov 19 '15 at 19:58
  • When I do NSLog [self.objectsTable ObjectAtIndex:rowIndex], I get ` (entity: Cschedule; id: 0x7875ee20 ; data: )` – bachma0507 Nov 19 '15 at 20:05
  • Check my answer and see if that works for you. I think self.objectsTable is filled with NSManagedObject's so you need to get the NSManagedObject then get the "date" key-value pair from the NSManagedObject. – MSU_Bulldog Nov 19 '15 at 20:14

2 Answers2

0

I think your problem is that you are assigning a string to an NSManagedObject. Try doing it like you did in your loop in updateWatchTable.

In your didSelectRowAtIndex method:

NSManagedObject *item = self.objectsTable objectAtIndex[rowIndex];
NSString * scheduleDate = [NSString stringWithFormat:@"%@",[item valueForKey:@"date"]];
MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
  • Unfortunately I'm still getting `Value for scheduleDate = (null)` after making the suggested changes above. – bachma0507 Nov 19 '15 at 20:35
0

The code below is what solved it for me. I created an array to store the MSManageObjects in the for loop. Then in the WKInterfaceTable didSelectRowAtIndex method I did an objectAtIndex: rowIndex on the array.

[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];
}
bachma0507
  • 1,543
  • 2
  • 11
  • 22