iOS,在表视图的每一行中添加新的Core Data对象

时间:2012-09-15 16:26:05

标签: ios xcode uitableview core-data nsmanagedobject

我正在尝试在Table View中添加一个新的Core Data对象,并尝试使用新数字命名每一行。类似于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];

}

0 个答案:

没有答案