ios - NSObject 在 ViewDidLoad 和 cellForRowAtIndexPath 之间更改属性值

标签 ios objective-c core-data

我通过 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/

相关文章:

ios - 使用GCD,但从服务器加载图像时我的应用仍然卡住

iOS Today Extension - 共享核心数据

objective-c - Core Data 中用于 C 结构的 NSValueTransformer

android - 为企业和客户提供单独的移动应用程序,还是使用不同登录名的单个应用程序?

ios - 导航 Controller 委托(delegate)方法没有得到调用

ios - swift 2 : Strange Objective C -> Swift method's signature conversion

ios - Facebook 登录按钮对齐 iOS

iphone - 导航栏外观

ios - 如何在 Swift 4 中使用 FetchedResultsController 获取 DateSectionTitles

ios - NSString 的逗号分隔天数到 NSDate 计算与当前日期最接近的日期