如何反映我的UITableView上添加的条目?

时间:2012-01-11 07:19:46

标签: iphone uitableview core-data ios5 storyboard

我正在使用核心数据和故事板。我有1 UITableView和1 UIViewController。当我向我的实体添加entires时,它会在我的上下文(数据库)中添加,但在我返回时不会反映在表视图中。我必须再次运行我的应用程序,然后我可以看到我添加的条目。这是我添加新对象的方式:

- (void)viewDidLoad
{
    [super viewDidLoad];

    myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    self.managedObjectContext = myAppDelegate.sharedManagedObjectContext;

    NSFetchRequest *personListRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *personEntity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:managedObjectContext];

    [personListRequest setEntity:personEntity];

    NSError *personListRequestError;

    NSArray *personsList = [self.managedObjectContext executeFetchRequest:personListRequest error:&personListRequestError];


    for(Person *thisPerson in personsList ) {
        NSLog(@"First name is %@", thisPerson.firstName);
        NSLog(@"First name is %@", thisPerson.lastName);
    }    
    NSLog(@"the contents of array are %@",personsList);
    personArray = [[NSMutableArray alloc] initWithArray:personsList];
}

我希望在添加它们时在我的表格中更新我的条目,我不想再次运行我的应用程序来查看它们。

2 个答案:

答案 0 :(得分:1)

使用获取的结果控制器有效地管理从Core Data获取请求返回的结果,以便为UITableView对象提供数据。

虽然可以通过多种方式使用表视图,但此对象主要用于帮助您获取主列表视图。 UITableView期望其数据源将单元格提供为由行组成的部分数组。您可以使用指定实体的获取请求,包含至少一个排序顺序的数组以及可选的过滤谓词来配置此类的实例。 NSFetchedResultsController有效地分析获取请求的结果,并计算结果集中的部分和索引的所有信息。有关详细信息apple docother sample

答案 1 :(得分:0)

你需要告诉tableview重新加载它的数据:

[tableView reloadData];