iOS,在表视图的每一行中添加新的核心数据对象
我试图在表视图中添加一个新的核心数据对象,并尝试用一个新的编号命名每一行。类似于Xcode中的主细节模板,但带有数字而不是日期。它应该以1开头,然后是2、3、4、5等等。但它当前添加了对象,但只显示数字1。也许这是我在逻辑上犯的一个简单错误,有人能指出并帮助我吗?代码如下:iOS,在表视图的每一行中添加新的核心数据对象,ios,xcode,uitableview,core-data,nsmanagedobject,Ios,Xcode,Uitableview,Core Data,Nsmanagedobject,我试图在表视图中添加一个新的核心数据对象,并尝试用一个新的编号命名每一行。类似于Xcode中的主细节模板,但带有数字而不是日期。它应该以1开头,然后是2、3、4、5等等。但它当前添加了对象,但只显示数字1。也许这是我在逻辑上犯的一个简单错误,有人能指出并帮助我吗?代码如下: -(void)addNewRow { // Create and configure a new instance of the Event entity. Hoyo *hoyo = (Hoyo *)[NSEnti
-(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];
}
在代码中,只插入一个对象。这是故意的吗?我对这段代码很好奇,纳什,在你的代码中不认识这段(大概是西班牙语)语法。具体地说,
[hoyo numero]
和[hoyo setNumero]
看起来可能是您自己在核心数据类中定义的,如果是这样的话,这无疑是另一个潜在失败的原因。如果问题仍然存在,你能解释一下你的代码吗?