UITextField委托中的核心数据更新导致崩溃

时间:2012-11-09 07:03:35

标签: ios ios5 core-data delegates

我正在研究应用程序并且只是编辑单元格文本字段并在完成编辑时,我正在尝试更新coredata但是应用程序获取堆栈并且有时它给SIGSTOP并且有时它说无法分配更多内存有点错误,请帮助我这个。

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ((textField.tag - 999) > 0) {
        NSArray *visible = [self.productTable indexPathsForVisibleRows];
        UITableViewCell* cell = [self.productTable cellForRowAtIndexPath:[visible objectAtIndex:(textField.tag - 999)]];

        for (UIView* subview in [cell.contentView subviews]) {
            if ([subview isKindOfClass:[UITextField class]]) {
                textField = (UITextField*)subview;
                Product* myProduct        = (Product*)[self.productArray objectAtIndex:(textField.tag - 1000)];
                [myProduct setValue:[NSNumber numberWithFloat:[textField.text floatValue]] forKey:@"quantity"];
                [myProduct setValue:[NSNumber numberWithFloat:[myProduct.quantity floatValue] * [myProduct.rate floatValue]] forKey:@"amount"];
                NSLog(@"Product %@", myProduct);

                NSError *error; if (![self.managedObjectContext save:&error]) {
                    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                    abort();
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的子视图循环似乎不是必需的 但是你的可见细胞检查已经存在缺陷。

  • 您将获得所有可见单元格的索引路径。
  • 您可以根据文本字段的标记选择其中一个索引路径。

- >这具有不可预测的结果。如果表视图已滚动,则可见单元格数组中单元格的确切索引不清晰。如果单元格已从屏幕滚动,则数组将为空并且您将崩溃。

为什么这么复杂?
您需要的文本字段不是传递给委托方法的文本字段吗?

所有你需要的是:

UITableViewCell *cell = (UITableViewCell*) textField.superview.superview;
Product *myProduct = [self.fetchedResultsController objectAtIndexPath:
   [self.tableView indexPathForCell:cell]];

您使用的是NSFetchedResultsController,对吧?如果没有,你肯定应该!

相关问题