NSFetchedResultsController帮助

时间:2011-06-28 15:26:04

标签: objective-c

我的表格视图没有显示任何内容(即使有数据),当我从标签栏切换回来时,我收到错误:

2011-06-28 11:25:20.043 Paparazzi [7773:207] - [__ NSCFArray sections]:无法识别的选择器发送到实例0x592d2d0 2011-06-28 11:25:20.048 Paparazzi [7773:207] * 由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFArray sections]:无法识别的选择器发送到实例0x592d2d0'

我做错了什么?

您可以在以下网址获取完整代码:

https://github.com/blasto333/Paparazzi

标题

@interface PersonListViewController : UITableViewController {
    NSFetchedResultsController *fetchResultsController;
}

@end

实施

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    fetchResultsController = [[FlickrFetcher sharedInstance] fetchedResultsControllerForEntity:@"Person" withPredicate:nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

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

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

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[fetchResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    Person *person = [fetchResultsController objectAtIndexPath:indexPath];
    [cell.textLabel setText:person.name];

    return cell;
}

1 个答案:

答案 0 :(得分:1)

您需要保留fetchResultsController,因为它一旦被创建就会被释放。方法fetchedResultsControllerForEntity:withPredicate返回一个自动释放的对象。将行更改为:

fetchResultsController = [[[FlickrFetcher sharedInstance] fetchedResultsControllerForEntity:@"Person" withPredicate:nil] retain];

iOS和Mac OS上强烈的内存管理很快就会成为过去。如果您是付费ADC成员,请查看Xcode的一些测试版并测试自动引用计数内容。我已经使用它几个星期了,它很棒。

编辑:您没有显示任何行,因为您的fetchResultsController中没有任何数据。方法numberOfSectionsInTableView:返回零,因此甚至没有调用TableViewDataSource协议的其他方法。