0

我有一个UITableView适用于我的 iPad 的应用程序,当您点击一行时,将显示一个 popOver,其中包含另一个表格,其中包含一个部分。popOver 表部分标题取决于fetchedResultsController主 UITableView 控制器上的值。当弹出框第一次加载时,信息是正确的。但是,在用户点击弹出框中的单元格后,它会转到弹出框内的另一个视图,该视图将允许您编辑数据。保存后,我会调用一个委托方法来刷新 main UITableView,同时弹出框保持显示。然后用户编辑数据的视图被关闭,因此它返回到 popover UITableView。此时fetchedResultsController传入的slot值现在为零。我无法弄清楚它为什么这样做。

为什么slot现在是零,SlotViewController我该如何防止它?

@界面

NSFetchedResultsController *fetchedResultsController;

主 UITableView

@synthesize fetchResultsController = _fetchedResultsController;

- (NSFetchedResultsController *)fetchedResultsController {
    /*
     Set up the fetched results controller.
     */
    if (_fetchedResultsController != nil) {
        NSLog(@"RETURNING FETCHEDRESULTS");
        return _fetchedResultsController;
    }
    NSLog(@"Should only be called once");
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = nil;
    if ([[dataObj.client objectForKey:@"appt_method"] integerValue] == 2) {
        entity = [NSEntityDescription entityForName:@"Slots" inManagedObjectContext:[[CoreDataHelper sharedInstance] managedObjectContext]];
    } else {
        entity = [NSEntityDescription entityForName:@"Appointments" inManagedObjectContext:[[CoreDataHelper sharedInstance] managedObjectContext]];
    }
    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:nil];
    //[fetchRequest setIncludesPendingChanges:YES];

    // Set the batch size to a suitable number.
    //[fetchRequest setFetchBatchSize:20];

    // Sort using the date / then time property.
    NSSortDescriptor *sortDescriptorDate = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
    NSSortDescriptor *sortDescriptorTime = [[NSSortDescriptor alloc] initWithKey:@"start_time" ascending:YES selector:@selector(localizedStandardCompare:)];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorDate, sortDescriptorTime, nil];


    [fetchRequest setSortDescriptors:sortDescriptors];

    // Use the sectionIdentifier property to group into sections.
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[[CoreDataHelper sharedInstance] managedObjectContext] sectionNameKeyPath:@"date" cacheName:nil];
//    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Slots *selectedSlot = [_fetchedResultsController objectAtIndexPath:indexPath];
    SlotViewController *slotView = [parentNav.storyboard instantiateViewControllerWithIdentifier:@"SlotViewController"];
            [slotView setSlot:selectedSlot];
    CGRect rect = [self rectForRowAtIndexPath:indexPath];
                rect.size.width = rect.size.width / 3;
                UINavigationController *navBar = [[UINavigationController alloc] initWithRootViewController:slotView];
                popOver = [[UIPopoverController alloc] initWithContentViewController:navBar];
                popOver.delegate = self;
                //[popOver setPopoverContentSize:CGSizeMake(320, 460) animated:YES];
                [popOver presentPopoverFromRect:rect inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

SlotViewController / popover UITableView

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSLog(@"slot: %@", _slot.date);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMdd"];
    NSDate *currDate = [dateFormatter dateFromString:_slot.date];

    [dateFormatter setDateFormat:@"h:mma"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    NSDate *midnight = [NSDate dateWithTimeIntervalSince1970:0];
    NSDate *startTimeDate = [midnight dateByAddingTimeInterval:[_slot.start_time integerValue] * 60];
    NSDate *endTimeDate = [midnight dateByAddingTimeInterval:[_slot.end_time integerValue] * 60];

    NSString *startTime = [dateFormatter stringFromDate:startTimeDate];
    NSString *endTime = [dateFormatter stringFromDate:endTimeDate];

    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [dateFormatter setDateFormat:@"E, MMM dd, yyyy"];
    return [NSString stringWithFormat:@"%@ %@-%@",[dateFormatter stringFromDate:currDate], startTime, endTime];
}

我可以通过更改didSelectRowAtIndexPath代码来使其正常工作

Slots *selectedSlot = [_fetchedResultsController objectAtIndexPath:indexPath];

Slots *selectedSlot = [Slots disconnectedEntity];
Slots *tmpSlot = [_fetchedResultsController objectAtIndexPath:indexPath];
for (id key in tmpSlot.entity.attributesByName) {
    [selectedSlot setValue:[tmpSlot valueForKey:key] forKey:key];
}

disconnectedEntity 是一个核心数据托管上下文,它不绑定到一个对象。

+ (id)disconnectedEntity {
    NSManagedObjectContext *context = [[CoreDataHelper sharedInstance] managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Slots" inManagedObjectContext:context];
    return [[self alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:nil];
}

但是我想知道为什么原始代码不起作用。我假设这是因为fetchedResultsController正在传递引用,selectedSlots所以当fetchedResultsController更新时,引用不再存在?

4

0 回答 0