Objective-C,iOS,核心数据,表格单元格为空



几个小时以来,我一直在努力解决这个问题,并试图找出这个代码的问题所在。我使用核心数据将标题和副标题保存在表格单元格中。我在我的应用程序委派(sharedAppDelegate)中有一些设置,在我的表视图中是我试图将数据插入单元格的情况。

以下是我在我的appDelegate.m:中的内容

+(StaticTableAppDelegate *)sharedAppDelegate
{
return sharedInstance;
}
-(NSArray *)allConsoles
{
NSManagedObjectContext *moc = [[StaticTableAppDelegate sharedAppDelegate] managedObjectContext];
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Homework" inManagedObjectContext:moc];
[fetch setEntity:entity];
//sort
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortdescriptors  = [NSArray arrayWithObject:sd];
[fetch setSortDescriptors:sortdescriptors];
NSError *error;
NSArray *result = [moc executeFetchRequest:fetch error:&error];
if(!result){
    NSLog(@"%@", [error localizedDescription]);
    return nil;
}
return result;
}

在视图中,我有这个(我导入了应用程序代理):

- (void)viewDidLoad
{
  [super viewDidLoad];
    NSManagedObjectContext *cxt = [[StaticTableAppDelegate sharedAppDelegate] managedObjectContext];
    NSError *error;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Homework" inManagedObjectContext:cxt];
   [request setEntity:entity];
   NSArray *arry = [cxt executeFetchRequest:request error:&error];
for( UserData *user in arry){
    NSLog(@"title %@", user.title);
    NSLog(@"details %@", user.details);
  }

    StaticTableAppDelegate *ad = [StaticTableAppDelegate sharedAppDelegate];
    NSArray *list = [ad allConsoles];

   if ([list count]==0) {
    tabledata = [[NSMutableArray alloc] init];
    tablesubtitles = [[NSMutableArray alloc]init];
      [tabledata addObject:@"hello2"];
      [tabledata addObject:@"hello2"];
      [tabledata addObject:@"hello2"];
      [tabledata addObject:@"hello2"];

      [tablesubtitles addObject:@"details"];
      [tablesubtitles addObject:@"details"];
      [tablesubtitles addObject:@"details"];
      [tablesubtitles addObject:@"details"];

      NSManagedObjectContext *moc = [ad managedObjectContext];

     for (int i=0; i<[tabledata count]; i++) {
     NSManagedObject *newTabledata = [NSEntityDescription insertNewObjectForEntityForName:@"Homework" inManagedObjectContext:moc];
     [newTabledata setValue:[NSString stringWithFormat:[tabledata objectAtIndex:i]] forKey:@"title"];

     NSManagedObject *newTablesub = [NSEntityDescription insertNewObjectForEntityForName:@"Homework" inManagedObjectContext:moc];
    [newTablesub setValue:[NSString stringWithFormat:[tablesubtitles objectAtIndex:i]] forKey:@"details"];

  }

    list = [ad allConsoles];
  }

 tabledata = [tabledata mutableCopy];
}

和表视图方法:

 - (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath: (NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"homeworkcell"];

if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"homeworkcell"];
}


cell.textLabel.text=[[tabledata objectAtIndex:indexPath.row] valueForKey:@"title"];
cell.detailTextLabel.text = [[tablesubtitles objectAtIndex:indexPath.row]  valueForKey:@"details"];
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
cell.textLabel.backgroundColor = [ UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
//-----------------------------------------START----------------------------Set image of cell----
cellImage = [UIImage imageNamed:@"checkboxblank.png"];
cell.imageView.image = cellImage;
//--------------------------------------------END---------------------------end set image of cell--  
return cell;

}

我很感激你的帮助。

这是如何存储到coreData。。。。如果你能为你的代码更改它。。

MortgageAppAppDelegate *MortDataObj=(MortgageAppAppDelegate *)[[UIApplication sharedApplication]delegate];
    NSManagedObjectContext *context = [MortDataObj managedObjectContext];
    loanDetails=[NSEntityDescription
                              insertNewObjectForEntityForName:@"LoanDetails" 
                              inManagedObjectContext:context]; 
    loanDetails.loanName = name;
    loanDetails.loanDescription = desc;

    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }

最新更新