简体   繁体   中英

NSObject changing property value between ViewDidLoad and cellForRowAtIndexPath

I'm passing an NSManagedObject to a view controller by ways of prepareForSegue.

The object I'm assigning to is declared in the receiving viewcontroller as such:

@property (retain, nonatomic) Allowance *allowanceSchedule;

Said object has a property called type which is an NSNumber. When I pass the object to the receiving viewcontroller, this property is set to 1 in the sending viewcontroller.

In in the receiving controller, in ViewDidLoad, I can confirm this property is indeed set to 1, but in cellForRowAtIndexPath the property is set to 0 (its coredata default). At no other point in the code I set this property. Any help is appreciated.

Here's the cellForRowAtIndexPath code:

- (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;

}

And here's 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;
}

}

My guess is that your object is getting faulted somewhere or you're manageObjectContext is getting invalidated. CoreData will spit out debug messages to your console, double check it and see if you have anything being printed.

Check to see if you're using the object on a different thread, or accidentally did a rollback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM