0

我正在开发一个 iPhone 应用程序,但发生了一些奇怪的事情。我想我可以向你征求意见。

我实现了一个 TableView 和一个自定义视图作为创建新对象表单。当 TableView 工具栏中的用户选项卡“添加”按钮时,我使自定义视图向上滑动,如下面的代码:

- (void)slide_up_form:(id)sender {

    CustomViewController *controller = [[CustomViewController alloc] init];

    // Assign this controller to use add_entity: message to TableView.
    controller.parent = self;

    [self presentModalViewController:controller animated:TRUE];
}

// To update TableView presentation.
-(void)add_block:(Block *)newBlock
{
 [blockArray insertObject:newBlock atIndex:0];
 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
 [self.tblCameraList insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                              withRowAnimation:UITableViewRowAnimationNone];

        [self.tblCameraList scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
                         atScrollPosition:UITableViewScrollPositionTop animated:YES];

 }

在自定义视图中,用户填写表单和选项卡“保存”按钮。所以我确实将实体保存到核心数据。并且需要用新对象更新 TableView,所以我在自定义视图中编写以下语句:

@synthesize parent;

- (void)save_block:(id)sender {
// save entity.

[self.parent add_block:newBlock];

// Slide down the custom view and show the TableView.
[[self parentViewController] dismissModalViewControllerAnimated:TRUE];
}

我已经在模拟器和 iPhone 中对此进行了测试。当用户选项卡“保存”按钮时,应用程序保存了新实体,但它不会使用新条目更新 TableView。

你能给你的建议来解决这个问题吗?

4

1 回答 1

0

看一下reloadDataUITableView 上的方法——它告诉视图重做所有导致它显示单元格的事情,从数据获取开始。假设您的表格视图的数据源使用您的核心数据存储来提供其数据,那么您的表格应该包含新对象。

或者,您可以让弹出控制器将新对象添加到表本身 - 为弹出控制器提供指向主控制器中表的指针,并使用 UITableView 的任何方法插入对象。只需确保您在表中插入的内容与您在 Core Data 存储中插入的内容一致。(您可以查看 Apple 的一些核心数据项目,例如 CoreDataRecipes,以了解更多信息。)

于 2009-10-02T06:40:26.597 回答