首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSFetchedResultsController帮助

NSFetchedResultsController帮助
EN

Stack Overflow用户
提问于 2011-06-28 15:26:04
回答 1查看 293关注 0票数 0

我的表视图没有显示任何内容(即使有数据),当我从选项卡栏切换回它时,会得到一个错误:

2011-06-28 11:25:20.043狗仔队7773:207个-__NSCFArray区段:未识别的选择器发送到实例0x592d2d0 2011-06-28 11:25:20.048狗仔队7773:207*终止应用程序由于非正常异常'NSInvalidArgumentException',原因:‘_NSCFArray区段:未识别的选择器发送到实例0x592d2 d0’

我做错了什么?

您可以在以下位置获得完整的代码:

https://github.com/blasto333/Paparazzi

标题

代码语言:javascript
复制
@interface PersonListViewController : UITableViewController {
    NSFetchedResultsController *fetchResultsController;
}

@end

实现

代码语言:javascript
复制
- (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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-28 16:59:13

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

代码语言:javascript
复制
fetchResultsController = [[[FlickrFetcher sharedInstance] fetchedResultsControllerForEntity:@"Person" withPredicate:nil] retain];

然而,iOS和Mac上的内存管理将很快成为过去。如果您是付费ADC成员,请查看Xcode的一些beta版本,并测试驱动自动参考计数的内容。我已经用了几个星期了,效果很好。

编辑:因为您的fetchResultsController中没有任何数据,所以不会显示任何行。方法numberOfSectionsInTableView:正在返回零,因此您的TableViewDataSource协议的其他方法都没有被调用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6508936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档