[英]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.
我的应用程序委托(sharedAppDelegate)中有一些设置,而我的表视图中是我尝试将数据插入到单元格中的设置。
Here is what I have in My appDelegate.m: 这是我的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: 和tableview方法:
- (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.. 这是存储到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]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.