【问题标题】:NSObject changing property value between ViewDidLoad and cellForRowAtIndexPathNSObject 在 ViewDidLoad 和 cellForRowAtIndexPath 之间更改属性值
【发布时间】:2014-07-24 07:19:12
【问题描述】:

我通过 prepareForSegue 将 NSManagedObject 传递给视图控制器。

我要分配的对象在接收视图控制器中声明如下:

@property (retain, nonatomic) Allowance *allowanceSchedule;

所述对象有一个名为 type 的属性,它是一个 NSNumber。 当我将对象传递给接收视图控制器时,该属性在发送视图控制器中设置为 1。

在接收控制器的 ViewDidLoad 中,我可以确认该属性确实设置为 1,但在 cellForRowAtIndexPath 中该属性设置为 0(其 coredata 默认值)。 在代码中没有其他地方我设置了这个属性。任何帮助表示赞赏。

这是 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;
}

}

【问题讨论】:

  • 请至少出示您的代码 - cellForRowAtIndexPath
  • 我已经用代码进行了编辑。谢谢。
  • 该值为零的可能性非常好,因为据说持有它的容器为零。
  • 值在哪里设置为1?
  • 是的,您正在分配一个新的核心数据对象并将其传递给您的视图控制器。我希望它为 0,因为您尚未将任何其他数据初始化到其中

标签: ios objective-c core-data


【解决方案1】:

我的猜测是您的对象在某处出现故障,或者您的 manageObjectContext 正在失效。 CoreData 将向您的控制台输出调试消息,仔细检查它,看看您是否打印了任何内容。

检查您是否在不同的线程上使用该对象,或者不小心进行了回滚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 2023-03-28
    • 2019-12-07
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    相关资源
    最近更新 更多