Core Dataに基づくUITableViewに挿入行を追加することに関する私の別の質問で、NSFetchedResultsControllerがフェッチする各オブジェクトをUITableViewの個別のセクションに割り当てていると述べました。これは単なるデフォルトの動作だと思いましたが、Marcus S. Zarraは、コントローラーの構成またはデータソースのデリゲートメソッドに問題がある可能性があると述べました。確かに、私のコードは、Appleのドキュメントと多数のチュートリアルから抜粋した部分を備えたFrankensteinに少し似ています。これは私の初めてのプログラムであり、Core Dataを使用するのは初めてなので、優しくしてください;)
私のテーブルビューコントローラーのヘッダーは次のとおりです。
#import <UIKit/UIKit.h>
#import "RubricAppDelegate.h"
@interface ClassList : UITableViewController {
NSMutableArray *classList;
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
}
@property(nonatomic,retain) NSMutableArray *classList;
@property(nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property(nonatomic, retain) NSManagedObjectContext *managedObjectContext;
- (IBAction) grade:(id)sender;
@end
私の実装ファイルには、ダミーのテストデータがたくさん含まれています。CoreDataオブジェクトをインスタンス化するために誤ったメソッドを使用している場合に備えてそれを含めました。基本的に、NSFetchedResultsControllerがオブジェクト(この場合はmyClassのインスタンス)を別々のセクションに返さないようにする必要があるかどうかを知りたいです。もしそうなら、私はその問題を作成するために何をしていますか?
現時点での最終的な目標は、テーブルの上部に挿入セルを追加できるようにすることです(挿入セルを下部に配置するのが「標準」であることはわかっていますが、それを実行するアプリでの外観は気に入っています。逆)。セクション0のセルスタイルを挿入するように設定していることに気付くでしょう-tableView:editingStyleForRowAtIndexPath:
が、もちろん、セル0ではなくセル1でmyClass.classTitleのリストを開始する方法を理解する必要があります(これが、各オブジェクトがあるかどうかを判断する理由です。独自のセクションに割り当てられるのは正常です)。
これが私の実装ファイルです:
#import "ClassList.h"
#import "ClassRoster.h"
#import "RubricAppDelegate.h"
#import "Student.h"
#import "myClass.h"
@implementation ClassList
@synthesize classList;
@synthesize fetchedResultsController;
@synthesize managedObjectContext;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.editing = YES;
RubricAppDelegate *appDelegate = (RubricAppDelegate *)[[UIApplication sharedApplication] delegate];
managedObjectContext = [appDelegate managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"myClass" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entity];
//test data
myClass *newClass = (myClass *) [NSEntityDescription insertNewObjectForEntityForName:@"myClass" inManagedObjectContext:managedObjectContext];
newClass.classTitle = @"UFDN 1000";
NSNumber *ID = [NSNumber numberWithInt:1];
newClass.classID = ID;
Student *newStudent = (Student *) [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:managedObjectContext];
newStudent.classID = ID;
newStudent.studentName = @"Andy Albert";
newStudent.studentUsername = @"albera";
[newClass addStudentsObject:newStudent];
newStudent.classID = ID;
newStudent.studentName = @"Bob Dole";
newStudent.studentUsername = @"doleb";
[newClass addStudentsObject:newStudent];
newStudent.classID = ID;
newStudent.studentName = @"Chris Hanson";
newStudent.studentUsername = @"hansoc";
[newClass addStudentsObject:newStudent];
myClass *newClass2 = (myClass *) [NSEntityDescription insertNewObjectForEntityForName:@"myClass" inManagedObjectContext:managedObjectContext];
newClass2.classTitle = @"UFDN 3100";
ID = [NSNumber numberWithInt:2];
newClass2.classID = ID;
newStudent.classID = ID;
newStudent.studentName = @"Danny Boy";
newStudent.studentUsername = @"boyd";
[newClass2 addStudentsObject:newStudent];
newStudent.classID = ID;
newStudent.studentName = @"James Matthews";
newStudent.studentUsername = @"matthj";
[newClass2 addStudentsObject:newStudent];
newStudent.classID = ID;
newStudent.studentName = @"Aaron Todds";
newStudent.studentUsername = @"toddsa";
[newClass2 addStudentsObject:newStudent];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"classID" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
NSError *error;
fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:@"classTitle" cacheName:nil];
[fetchedResultsController performFetch:&error];
UIBarButtonItem *gradeButton = [[UIBarButtonItem alloc]
initWithTitle:@"Grade"
style:UIBarButtonItemStylePlain
target:self
action:@selector(grade:)];
self.navigationItem.rightBarButtonItem = gradeButton;
[gradeButton release];
}
- (IBAction) grade:(id)sender {
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"Number of sections = %d", [[fetchedResultsController sections] count]);
return ([[fetchedResultsController sections] count]);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> myClass = [[fetchedResultsController sections] objectAtIndex:section];
NSLog(@"Number of classes = %d", [myClass numberOfObjects]);
return [myClass numberOfObjects];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
myClass *theClass = [fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"Class name is: %@", theClass.classTitle);
cell.textLabel.text = theClass.classTitle;
}
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return UITableViewCellEditingStyleInsert;
}
else return UITableViewCellEditingStyleDelete;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
myClass *result = (myClass *)[fetchedResultsController objectAtIndexPath:indexPath];
[managedObjectContext deleteObject:result];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
}
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[classList release];
[super dealloc];
}
@end
私のRubricAppDelegateは、コアデータNSManagedObjectContext、NSPersistentStoreCoordinatorなどを設定するためのAppleのドキュメントと基本的に同じです。ただし、問題があると思われる場合は、お知らせください。投稿します。
編集:各オブジェクトが異なるセクションに割り当てられていることを知っている2つの理由について言及するのを忘れました。
1)NSLog(@"Number of sections = %d", [[fetchedResultsController sections] count]);
insideは-numberOfSectionsInTableView:
、私が持っているmyClassオブジェクトの数を返します。
2)-numberOfSectionsInTableView:
1を返すように設定した場合、テーブルには1つのオブジェクトのみが表示され、残りは切り取られます。