【问题标题】:data not reloading into tableview from core data on minor update数据不会在次要更新时从核心数据重新加载到 tableview
【发布时间】:2011-02-22 21:33:40
【问题描述】:

我有一个基本的相册应用程序,在第一次查看时会显示相册列表,并带有一个字幕,显示每个相册中有多少张图片。我已经完成了添加相册和向相册添加图像的所有工作。 问题是应用程序加载时图像计数行是准确的,但我无法在执行期间更新它们。

以下 viewdidload 在应用加载时正确填充了 tableview 的所有行:

- (void)viewDidLoad {
    [super viewDidLoad]; 
    // Set the title.
    self.title = @"Photo albums";

    // Configure the add and edit buttons.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAlbum)];
    addButton.enabled = YES;
    self.navigationItem.rightBarButtonItem = addButton;

    /*
    Fetch existing albums.
    Create a fetch request; find the Album entity and assign it to the request; add a sort descriptor; then execute the fetch.
    */
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];

    // Order the albums by name.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"albumName" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptor release];
    [sortDescriptors release];

    // Execute the fetch -- create a mutable copy of the result.
    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil) {
        // Handle the error.
    }

    LocationsAppDelegate *mainDelegate = (LocationsAppDelegate *)[[UIApplication sharedApplication] delegate];

    // Set master albums array to the mutable array, then clean up.
    [mainDelegate setAlbumsArray:mutableFetchResults];
    [mutableFetchResults release];
    [request release];
}

但是当我在 viewdidappear 中运行类似的代码时,什么也没有发生:

{
    /*
    Fetch existing albums.
    Create a fetch request; find the Album entity and assign it to the request; add a sort descriptor; then execute the fetch.
    */
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];

    // Order the albums by creation date, most recent first.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"albumName" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptor release];
    [sortDescriptors release];

    // Execute the fetch -- create a mutable copy of the result.
    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil) {
        // Handle the error.
    }

    LocationsAppDelegate *mainDelegate = (LocationsAppDelegate *)[[UIApplication sharedApplication] delegate];

    // Set master albums array to the mutable array, then clean up.
    [mainDelegate setAlbumsArray:mutableFetchResults];
    [self.tableView reloadData];
    [mutableFetchResults release];
    [request release];  
}

抱歉,如果我在其他地方错过了这个问题的答案,但我错过了什么?

【问题讨论】:

    标签: iphone objective-c uitableview


    【解决方案1】:

    要进行故障排除,请将NSLog 语句放在-viewDidAppear: 的几个位置。您将使用这些日志语句来确保此方法被正确调用并至少检查mutableFetchResults 的内容。

    【讨论】:

    • 谢谢,我没有在运行时保存要存储的数据,我只是在退出时保存更改。现在工作正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    相关资源
    最近更新 更多