私はこの問題を何時間も解決し、このコードで何が間違っているかを調べようとしています。私はテーブルのセルにタイトルとサブタイトルを保存するためにコアデータを使用しています。 私のアプリケーションデリゲート(sharedAppDelegate)にいくつかの設定があり、私のセルにデータを挿入しようとしていました。ここで Objective-C、iOS、Core Data、Table Cells empty
は、私は私のappDelegate.mに持っているものです。- (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];
}
そしてテーブルビュー方法:
+(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;
}
そして、私はこれを持っているビューに(私は、アプリのデリゲートをインポート)
- (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に保存するcoredata..Becauseあなたのコードの中に保存して、問題を持つ願ってい
されます完全に役に立たず、正しい方法ではありません。 –
これは役に立たないコメントでしたが、感謝しています。 –
あなたの質問は少し不明です。データの保存やデータの表示に問題がありますか? – sosborn