我通过 prepareForSegue 将 NSManagedObject 传递给 View Controller 。
我要分配的对象在接收 View Controller 中声明如下:
@property (retain, nonatomic) Allowance *allowanceSchedule;
所述对象有一个名为 type 的属性,它是一个 NSNumber。 当我将对象传递给接收 View Controller 时,此属性在发送 View Controller 中设置为 1。
在接收 Controller 中,在 ViewDidLoad 中,我可以确认此属性确实设置为 1,但在 cellForRowAtIndexPath 中,该属性设置为 0(其核心数据默认值)。 我没有在代码的其他位置设置此属性。感谢您的帮助。
这是 cellForRowAtIndexPath 代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
if (indexPath.section == 0) { // Allowance type
if (indexPath.row == [self.allowanceSchedule.type integerValue])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if (indexPath.section == 1) { // Week starts on
cell.detailTextLabel.text = [Time weekStartDescriptionFromIndex:[self.allowanceSchedule.weekStart integerValue]];
}
return cell;
这里是 prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqual: @"allowance"]) {
AllowanceSetupTableViewController *allowanceViewController = (AllowanceSetupTableViewController *)segue.destinationViewController;
allowanceViewController.delegate = self;
if (self.child.allowanceSchedule == nil) {
self.child.allowanceSchedule = [NSEntityDescription insertNewObjectForEntityForName:@"Allowance" inManagedObjectContext:self.managedObjectContext];
}
allowanceViewController.allowanceSchedule = self.child.allowanceSchedule;
}
最佳答案
我的猜测是您的对象在某处出现故障,或者您的 manageObjectContext 正在失效。 CoreData 将向您的控制台输出调试消息,仔细检查它并查看您是否正在打印任何内容。
检查您是否在不同的线程上使用该对象,或者不小心进行了回滚。
关于ios - NSObject 在 ViewDidLoad 和 cellForRowAtIndexPath 之间更改属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24904740/