2010-12-03 97 views
4

我想編輯一個CoreData對象,當用戶單擊UITableView中的一個單元格基於cell.accessoryType來顯示該項目是否已被點擊。這是當前的代碼。更新/編輯核心數據管理對象

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

NSManagedObject *itemToUpdate = [groceryArray objectAtIndex:indexPath.row]; 
NSLog(@"updating: %@", itemToUpdate); 

if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    itemToUpdate.purchased = NO; 
}else { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    itemToUpdate.purchased = YES; 
} 

// Commit the change. 
NSError *error; 
if (![managedObjectContext save:&error]) { 
    // Handle the error. 
    NSLog(@"Saving changes failed: %@", error); 

} 
} 

這似乎是選擇合適的對象,因爲的NSLog()會顯示正確的項目,但是當我嘗試使用點符號例如更新「itemToUpdate.purchased = YES;」編譯器會在「不是結構或聯合」的東西中拋出一個錯誤「購買成員的請求」。

我知道我可能做錯了(我在xcode中的第一個項目) - 任何意見將不勝感激!

感謝

回答

6

你試過:

[itemToUpdate setValue:[NSNumber numberWithBool:NO] forKey:@"purchased"] 

形式?

我總是子類NSManagedObject和點符號適用於聲明的屬性。但是你可以試試這個「老」的符號來看看它是否有效。

+0

謝謝!這工作...我有很多閱讀要做。感謝你的時間和幫助。 :) – lostincode 2010-12-03 09:48:31

0

我想你創建了'NSManagedObject'的自定義子類,其中'購買'作爲其中一個屬性。聲明'itemToUpdate'作爲這個子類的對象,而不是NSManagedObject:

YourCustomSubclassOfNSManagedObject *itemToUpdate = [groceryArray objectAtIndex:indexPath.row];