WKInterfaceTable didSelectRowAtIndex 中 Context 中的空值
Null value in Context in WKInterfaceTable didSelectRowAtIndex
我安装了一个 watchkit table,它工作正常。但是当我尝试将 WKInterfaceTable didSelectRowAtIndex 方法与上下文应该存在的值一起使用时,上下文给出了空值。我创建的测试上下文值工作正常并且该方法工作并推送正确填充的 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
}
我认为您的问题是您正在将字符串分配给 NSManagedObject。尝试像在 updateWatchTable
.
中的循环中那样做
在您的 didSelectRowAtIndex
方法中:
NSManagedObject *item = self.objectsTable objectAtIndex[rowIndex];
NSString * scheduleDate = [NSString stringWithFormat:@"%@",[item valueForKey:@"date"]];
下面的代码为我解决了这个问题。我创建了一个数组来将 MSManageObjects 存储在 for 循环中。然后在 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];
}
我安装了一个 watchkit table,它工作正常。但是当我尝试将 WKInterfaceTable didSelectRowAtIndex 方法与上下文应该存在的值一起使用时,上下文给出了空值。我创建的测试上下文值工作正常并且该方法工作并推送正确填充的 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
}
我认为您的问题是您正在将字符串分配给 NSManagedObject。尝试像在 updateWatchTable
.
在您的 didSelectRowAtIndex
方法中:
NSManagedObject *item = self.objectsTable objectAtIndex[rowIndex];
NSString * scheduleDate = [NSString stringWithFormat:@"%@",[item valueForKey:@"date"]];
下面的代码为我解决了这个问题。我创建了一个数组来将 MSManageObjects 存储在 for 循环中。然后在 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];
}