2010-12-03 96 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];