简体   繁体   English

数据表属性未出现在表视图控制器中

[英]data table attributes are not appearing in Table View Controller

I am new to iOS programming so hopeful that someone can help me with this... 我是iOS编程的新手,所以希望有人可以帮助我...

I am using a Table View Controller to display the contents from a data table created using the data model. 我正在使用表视图控制器来显示使用数据模型创建的数据表中的内容。 I have a View Controller that segues to the Table View Controller , which begins by creating the context: 我有一个View Controller可以选择Table View Controller ,它首先创建上下文:

- (void)viewDidLoad {
[super viewDidLoad];

//create managed document memory block
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsDirectory = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
NSString *documentName = @"MyDatabaseTest";
NSURL *url = [documentsDirectory URLByAppendingPathComponent:documentName];
UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:url];

//check to see if file already exists
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[url path]];

//if yes, do something, if not open/save
if (fileExists) {
    [document openWithCompletionHandler:^(BOOL success) {
        if (success) [self documentIsReady:document];
        if (!success) NSLog(@"couldn't open document at %@", url);
    }];
}  else  {
    [document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
        if (success) [self documentIsReady:document];
        if (!success) NSLog(@"couldn't open document at %@", url);
    }];
}

}

- (void)documentIsReady:(UIManagedDocument *)document  {

if (document.documentState == UIDocumentStateNormal)  {
    NSManagedObjectContext *context = document.managedObjectContext;
    self.context = context;

}

}

Then add the object in the segue: 然后将对象添加到segue中:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([sender isKindOfClass:[UIButton class]])  {
    NSIndexPath *indexPath = sender;
    NSDictionary *currentDictionaryItem = self.projectDictionary;
    NSString *currentName = [currentDictionaryItem valueForKeyPath:@"name"];

    if (indexPath)  {
        if ([segue.identifier isEqualToString:@"TableSegue"])  {
            [segue.destinationViewController setTitle:currentName];
//                NSLog(@"the mutable dictionary includes = %@",currentName);

            [Project newProjectCreation:self.projectDictionary inManagedObjectContext:self.context];

            ProjectsCDTVC *pcdtvc = [[ProjectsCDTVC alloc] init];
            [pcdtvc makeContext:self.context];

        }
    }
  }
}

The object appears to be added to the data table correctly, but when I call the Core Data Table View Controller subclass, ProjectsCDTVC , cellForRowAtIndexPath never gets called. 该对象似乎已正确添加到数据表中,但是当我调用核心数据表视图控制器子类ProjectsCDTVC时 ,永远不会调用cellForRowAtIndexPath。 ProjectsCDTVC is designated as the Custom Class for the Table View Controller in the storyboard as well. ProjectsCDTVC也被指定为情节提要中 Table View Controller自定义类

-(void)makeContext:(NSManagedObjectContext *)myContext
{

_managedObjectContext = myContext;

//request into Project table
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Project"];


//get all projects when predicate is nil
request.predicate = nil;
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedStandardCompare:)]];
request.fetchLimit = 100;

self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
   NSLog(@"the managed object context is %@",self.managedObjectContext);
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Project Cell"];

Project *project = [self.fetchedResultsController objectAtIndexPath:indexPath];

cell.textLabel.text = project.name;

return cell;
}

I saw a related post that noted that if the number of sections in the table view is zero that cellForRowAtIndexPath will not get called so I watched the sections variable. 我看到了一篇相关的文章,该文章指出,如果表视图中的节数为零,则不会调用cellForRowAtIndexPath,因此我观察了这些节变量。 Sections initially had a correct value of '1' but, at some point, got reset to '0'. 这些节最初的正确值为“ 1”,但在某些时候,已重置为“ 0”。 I have been debugging for some time and cannot locate why sections is getting reset so any help would be much appreciated! 我已经调试了一段时间,无法找到重新设置节的原因,所以我们将不胜感激!

You're passing the context to a newly created instance of ProjectsCDTVC that you never use, and will be immediately deallocated. 您会将上下文传递给从未使用过的新创建的ProjectsCDTVC实例,该实例将被立即释放。 You need to pass it to the segue.destinationViewController. 您需要将其传递给segue.destinationViewController。

ProjectsCDTVC *pcdtvc = segue.destinationViewController;
[pcdtvc makeContext:self.context];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM