简体   繁体   中英

Objective-C, iOS, Core Data, Table Cells empty

I've been trying to get through this for a couple of hours now and trying to figure out what is wrong with this code. I'm using core data to save title and a subtitle in a table cell. I have some setups in my app delegate(sharedAppDelegate) and in my tableview is were I try to inset data into my cells.

Here is what I have in My 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;

}

And in the view I have this(I imported app delegate):

- (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];

}

And tableview method:

 - (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;

}

I appreciate the help.

This is how to store into coreData....If u can change it for your code..

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]);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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