我的表视图没有显示任何内容(即使有数据),当我从选项卡栏切换回它时,会得到一个错误:
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
标题
@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;
}发布于 2011-06-28 16:59:13
您需要保留fetchResultsController,因为它一创建就会发布。方法fetchedResultsControllerForEntity:withPredicate返回一个自动释放的对象。将行改为:
fetchResultsController = [[[FlickrFetcher sharedInstance] fetchedResultsControllerForEntity:@"Person" withPredicate:nil] retain];然而,iOS和Mac上的内存管理将很快成为过去。如果您是付费ADC成员,请查看Xcode的一些beta版本,并测试驱动自动参考计数的内容。我已经用了几个星期了,效果很好。
编辑:因为您的fetchResultsController中没有任何数据,所以不会显示任何行。方法numberOfSectionsInTableView:正在返回零,因此您的TableViewDataSource协议的其他方法都没有被调用。
https://stackoverflow.com/questions/6508936
复制相似问题