使用核心数据的View Controller中的表视图

时间:2013-06-20 13:34:29

标签: core-data tableview

我正在使用核心数据,并尝试将表格视图单元格添加到视图控制器中的表格视图中。当我在表视图控制器中实现它时,应用程序运行。当我在表视图是另一个视图控制器的一部分时运行它时,应用程序不会运行。它在代码中显示错误:在DeviceViewController类型的对象上找不到属性tableview。 DeviceViewController.h具有表视图数据源和委托。我的代码是:

 #import "DeviceViewController.h"
#import "DeviceDetailViewController.h"

 @interface DeviceViewController ()
@property (strong) NSMutableArray *devices;
@end

@implementation DeviceViewController

- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];

// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}     

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

 // Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];
self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil]             mutableCopy];

[self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.devices.count;
}

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

// Configure the cell...
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@ ", [device valueForKey:@"name"]]];

return cell;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}


  - (void)tableView:(UITableView *)tableView commitEditingStyle:        (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];

if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[context deleteObject:[self.devices objectAtIndex:indexPath.row]];

NSError *error = nil;
   if (![context save:&error]) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}

// Remove device from table view
[self.devices removeObjectAtIndex:indexPath.row];
[self. otableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]          withRowAnimation:UITableViewRowAnimationFade];
  }
}

2 个答案:

答案 0 :(得分:0)

在我看来,在这种情况下,它与CoreData无关。

您应该将UITableViewDataSourceUITableViewDelegate协议添加到VC。 之后,您应该创建一个UITableView(您可以在界面构建器中执行此操作)并将数据源和委托连接到您的视图控制器。

您还可以在VC中为UITableView添加属性。

当您使用UITableViewController时,所有这些都是从Xcode完成的,但如果您在常规UITableView中使用UIViewController,则应该处理这些内容。

祝你好运!

编辑:

将其添加为属性

@property (nonatomic, strong) UITableView *deviceTableView;

viewDidLoad方法更改为:

- (void)viewDidLoad {
    [super viewDidLoad];
    _deviceTableView = [[UITableView alloc] initWithFrame:self.view.frame];
    _deviceTableView.delegate = self;
    _deviceTableView.dataSource = self;
    [self.view addSubview:_deviceTableView];
    [_deviceTableView reloadData];
} 

删除您在viewDidAppear中撰写的内容,并将self.tableView替换为此文件中的_deviceTableView

答案 1 :(得分:0)

可能导致崩溃的另一个问题是,当您尝试执行获取请求时,NSManagedObjectContext为nil。我想添加一个if([self managedObjectContext]) 在任何视图层次结构方法中使用它之前。

此外,在使用从故事板实例化的视图时应该小心。 Nikola的回答是在viewDidLoad中添加配置视图控制器的代码,只要你没有将UITableView拖到界面构建器中的UIViewController实例上就可以了。如果您已经在界面构建器中创建并链接了一个tableview,那么您将不必要地将它替换为一个新的,因此在     _deviceTableView = [[UITableView alloc] initWithFrame:self.view.frame]; 你应该添加一个if(!_deviceTableView) 声明,因为它会阻止您向视图添加第二个表视图。问题是,如果您已经在IB中添加了一个tableview,即使您更改了tableview属性,它也会被您的视图保留,因此可以显示两个表。

或者,您可以在getter方法中为表视图属性使用“lazy instantiation”,以确保在访问它时初始化它。

最后,为了帮助我们更好地了解您的应用崩溃的原因,从Xcode中的控制台获取异常日志会很有帮助。