2012-11-09 33 views
0

我工作的應用程序,只需編輯單元格文本字段,並在完成編輯,我試圖更新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(); 
       } 
      } 
     } 
    } 
} 
+0

任何人都可以幫助我呢? – Retro

+0

http://grab.by/howq。 「 – Retro

+1

」textField =(UITextField *)subview;「要覆蓋委託的對象,嘗試使用不同的指針(如*的UITextField = thisTextField(的UITextField *)子視圖;) - 不知道如果是這樣的問題(或者即使是故意的),但它是一個嘗試。 – jimpic

回答

0

看來你的子視圖循環確實沒有必要。
但你已經檢查可見細胞似乎有缺陷。

  • 您得到所有​​可見單元格的索引路徑。
  • 你選擇基於文本字段的標籤,這些指數的路徑之一。

- >這有不可預知的結果。如果表視圖已滾動,則可見單元格數組中的單元格的確切索引不清晰。如果單元格已經從屏幕上滾動下來,數組將會是空的,並且會導致崩潰。

爲什麼這麼複雜?
是不是你需要的文本字段正好是傳遞給委託方法的文本字段?

所有你需要的是這樣的:

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

您使用的是NSFetchedResultsController,對不對?如果沒有,你絕對應該!

+0

是的,我只是希望每當任何文本字段被編輯和完成編輯,然後我可以更新值。 plases告訴我一個更好的方法來做到這一點 – Retro

+0

@ user1653795看到我的編輯。 – Mundi

+0

不,我不是,PLZ指導我如何使用 – Retro