我试图在表视图中添加一个新的核心数据对象,并尝试用一个新数字命名每一行。类似于 Xcode 中的 Master Detail 模板,但带有数字而不是日期。它应该以 1 开头,然后是 2、3、4、5 等。但它目前添加了对象,但只显示数字 1。也许这是我在逻辑上犯的一个简单错误,有人可以指出和帮助?这是代码:
-(void)addNewRow {
// Create and configure a new instance of the Event entity.
Hoyo *hoyo = (Hoyo *)[NSEntityDescription insertNewObjectForEntityForName:@"Hoyo" inManagedObjectContext:context];
//Here I am trying to get the current value in the object which is 0
NSNumber *coreNumber = [NSNumber numberWithInt:[[hoyo numero]integerValue]];// 0
NSNumber *myNum = [[NSNumber alloc]initWithInt:0];//0
int currentNumber = [coreNumber integerValue] + 1;//0 +1 = 1
NSNumber *newNum = [[NSNumber alloc]initWithInt:currentNumber];
NSLog(@"El numero de hoyo es: %@", [hoyo numero]);
if (([[hoyo numero] isEqualToNumber:coreNumber]) || ([[hoyo numero] isEqualToNumber:myNum])) {
[hoyo setNumero:newNum];
[array insertObject:hoyo atIndex:0];
}
NSError *error = nil;
if (![context save:&error]) {
// Handle the error.
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}