Objective c NSFetchedResultsController帮助
我的表视图没有显示任何内容(即使有数据),当我从选项卡栏切换回该视图时,出现错误: 2011-06-28 11:25:20.043狗仔队[7773:207]-[\uu nscarray sections]:未识别的选择器发送到实例0x592D0 2011-06-28 11:25:20.048狗仔队[7773:207]*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[\uu nscarray sections]:未识别的选择器发送到实例0x592D0' 我做错了什么 您可以通过以下网址获取完整代码: 标题Objective c NSFetchedResultsController帮助,objective-c,Objective C,我的表视图没有显示任何内容(即使有数据),当我从选项卡栏切换回该视图时,出现错误: 2011-06-28 11:25:20.043狗仔队[7773:207]-[\uu nscarray sections]:未识别的选择器发送到实例0x592D0 2011-06-28 11:25:20.048狗仔队[7773:207]*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[\uu nscarray sections]:未识别的选择器发送到实例0x592
@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;
}
-(void)viewDidLoad
{
[超级视图下载];
//取消对下一行的注释以保留演示文稿之间的选择。
//self.clearselectiononviewwillappear=否;
//取消对以下行的注释,以在此视图控制器的导航栏中显示编辑按钮。
//self.navigationItem.rightBarButtonItem=self.editButtonItem;
fetchResultsController=[[FlickrFetcher sharedInstance]fetchedResultsControllerForEntity:@“Person”,谓词为nil];
}
-(无效)视图卸载
{
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
}
-(无效)视图将显示:(BOOL)动画
{
[超级视图将显示:动画];
}
-(无效)视图显示:(BOOL)动画
{
[超级视图显示:动画];
}
-(无效)视图将消失:(BOOL)已设置动画
{
[超级视图将消失:动画];
}
-(无效)视图消失:(BOOL)已设置动画
{
[超级视窗消失:动画];
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
//对于支持的方向返回YES
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回[[fetchResultsController节]计数];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
id sectionInfo=[[fetchResultsController节]对象索引:节];
返回[sectionInfo numberOfObjects];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier]自动释放];
}
//配置单元格。。。
Person*Person=[fetchResultsController对象索引路径:indexPath];
[cell.textlab setText:person.name];
返回单元;
}
您需要保留fetchResultsController,因为它一创建就被释放。方法fetchedResultsControllerForEntity:withPredicate
返回一个自动删除的对象。将行更改为:
fetchResultsController = [[[FlickrFetcher sharedInstance] fetchedResultsControllerForEntity:@"Person" withPredicate:nil] retain];
不过,iOS和Mac OS上的密集内存管理很快就会成为过去。如果你是一个付费的ADC成员,去看看Xcode的一些测试版,试驾一下自动参考计数的东西。我已经用了几个星期了,感觉很棒
编辑:由于fetchResultsController中没有任何数据,因此无法显示任何行。方法
numberOfSectionsInTableView:
返回零,因此TableViewDataSource协议的其他方法都没有被调用。自从雪豹之后,我就一直在使用垃圾收集,所以对于Mac OS开发人员来说,这已经是过去的事了。没错,但对于iOS来说,垃圾收集不是一个选项,ARC确实是最好的答案。即使Mac OS应用程序在没有自动释放池(即垃圾收集)的情况下也会表现得更好。我认为没有理由不将所有iOS/Mac OS应用程序转换为ARC。我的数据库(temp.db)中有数据,fetchResultsController中没有数据的原因是什么?我也没有。我只是说,你不必在桌面上进行任何内存管理。另外-垃圾收集不是自动释放池。如果您使用fetchManagedObjectsForEntity:withPredicate:
,并将结果存储在实例变量中(我称我的“people”)。我能让他们出现。我不知道NSFetchedResultsController的细节,但您一定缺少一些实现细节。在这里阅读文档:或者只需调用fetchManagedObjectsForEntity:withPredicate:
在视图中将显示:
,以便数据始终是最新的。