UITableView在退出并重新打开之前不显示结果

时间:2011-11-15 00:16:09

标签: iphone uitableview core-data nsfetchedresultscontroller nsmanagedobject

我正在使用NSFetchedResultsController来管理Gift对象的UITableViewList。想法是让用户通过单击导航栏上的添加按钮来添加礼物。添加按钮应该推送视图以将Gift配置到navigationController堆栈上。然后,如果用户实际将数据输入到字段中,它应该保存礼物,以便如果用户弹出视图(即导航回列表),他应该在列表中看到他的新礼物。很简单。问题是,除非我重新启动应用程序,否则主列表似乎不会更新。完全迷失了。

当用户点击主tableview(giftlistcontroller.m)上的“+”按钮时调用

-(void) addgift{
    Gift *newGift = [NSEntityDescription insertNewObjectForEntityForName:@"Gift" inManagedObjectContext:self.managedObjectContext];
    GiftEditController *editController = [[GiftEditController alloc] initWithStyle:UITableViewStyleGrouped];
    editController.gift = newGift;

    [self.navigationController pushViewController:editController animated:YES];
    [editController release];
}

然后,在GiftEditController中,如果用户更新字段...

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == nameField) {
        gift.name = textField.text;
    } else if (textField == priceField) {
        gift.price = [NSNumber numberWithFloat:[textField.text floatValue]];
    } 

    NSError *error;
    if (![gift.managedObjectContext save:&error]) {
        NSLog(@"Couldn't save in textFieldDidEndEditing");
    }
}

所有标准的fetchedresultscontroller委托方法都在giftlistcontroller中定义。完全难倒,任何建议都会很棒(!!!!)赞赏。

3 个答案:

答案 0 :(得分:1)

我没有看到你在任何地方重新加载桌子。将[myTableView reloadData];添加到您的代码中,以使表格“刷新”而无需退出应用。

答案 1 :(得分:1)

感谢大家的帮助。我终于找到了解决方案,我认为将此问题发布给任何有这个问题的人都是明智的(一个简单的谷歌搜索让人觉得有不止一些)。

你是对的,我需要重装我的桌子。但这还不够。问题是我刚刚删除了苹果使用的样板 fetchedResultsController 代码,这在这种情况下不起作用。特别是,fetchedResultsController的前几行读取:

-(NSFetchedResultsController *)fetchedResultsController
    if (fetchedResultsController != nil)
    {
        return fetchedResultsController;
    }

    ....
}

问题是,即使刷新表,它仍然使用旧的fetchedResultsController,它不知道新条目。我能够通过更新 viewDidAppear 中的fetchedResultsController来解决这个问题:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // Set the fetched results controller to nil in case we are coming back
    // from a view that added or removed entries.
    self.fetchedResultsController = nil;

    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    //reload the table to catch any changes
    [self.tableView reloadData];

}

谢谢James Webster和Louie。你们是救生员。

答案 2 :(得分:0)

  

UITableView会覆盖UIView的layoutSubviews方法,以便仅在创建UITableView的新实例或分配新数据源时调用reloadData。重新加载表视图会清除当前状态,包括当前选择。但是,如果显式调用reloadData,则会清除此状态,并且对layoutSubviews的任何后续直接或间接调用都不会触发重新加载。

[Source]

简而言之:致电[tableView reloadData]强制重新加载

相关问题