加载视图时,我将下列子类别管理对象加载到数组中。如何根据核心数据中的关系对象创建UITableView部分
@class Aircraft;
@interface AircraftTypes : NSManagedObject
@property (nonatomic, retain) NSNumber * isIFRCapable;
@property (nonatomic, retain) NSString * model;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * numEngines;
@property (nonatomic, retain) NSString * type;
@property (nonatomic, retain) Aircraft *tailNumber;
@property (nonatomic, retain) NSNumber *sortOrder;
@end
和
@class AircraftTypes;
@interface Aircraft : NSManagedObject
@property (nonatomic, retain) NSString * homeStation;
@property (nonatomic, retain) NSNumber * isSimulator;
@property (nonatomic, retain) NSString * owner;
@property (nonatomic, retain) NSString * tailNumber;
@property (nonatomic, retain) AircraftTypes *aircraftType;
@property (nonatomic, retain) NSManagedObject *loggedFlights;
@end
的关系是
AircraftTypes <--->> Aircraft
我所能,并已成功地与孩子玩耍的对象中的UITableView细胞的关系中,并segueing对其进行编辑。我想要做的是如何根据AircraftType成功创建UITableView部分,并使用属于该类型的飞机填充每个部分,并根据哪些行被删除成功地引用原始数组(索引)。这是我迄今为止,但我卡住了。
获取数组:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.managedObjectContext = [(ATPAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
managedObjectContext = self.managedObjectContext;
self.aircraftList = [CoreDataHelper getObjectsForEntity:@"Aircraft" withSortKey:@"tailNumber" andSortAscending:YES andContext:self.managedObjectContext];
self.aircraftTypeList = [CoreDataHelper getObjectsForEntity:@"AircraftTypes" withSortKey:@"sortOrder" andSortAscending:YES andContext:self.managedObjectContext];
}
加载数据(这整个区块工作至今):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [aircraftTypeList count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// return the title of an individual category
AircraftTypes *thisType = [self.aircraftTypeList objectAtIndex:section];
return [NSString stringWithFormat:@"%@ %@", thisType.type, thisType.model];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
AircraftTypes *thisType = [aircraftTypeList objectAtIndex:section];
int count = 0;
for (Aircraft *thisAircraft in self.aircraftList) {
if (thisAircraft.aircraftType == thisType) {
count++;
}
}
if (self.editing) { //Add insert row
count++;
}
return count;
}
这一个是造成错误,我做了一些数据预装入堆栈,并保存在应用程序中成功DidFinishLaunchingWithOptions
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"AircraftCell";
ATPSettingsMyAircraftTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == Nil) {
cell = [[ATPSettingsMyAircraftTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifier];
}
AircraftTypes *thisType = [aircraftTypeList objectAtIndex:indexPath.section];
cell.labelTailNumber.text = thisType.tailNumber.tailNumber;
cell.labelHomeStation.text = thisType.tailNumber.homeStation;
cell.labelAircraftModel.text = [NSString stringWithFormat:@"%@ %@",
thisType.type, thisType.model];
cell.labelHours.text = @"";
//calcuate aircraft hours later
return cell;
}
而且我甚至没有开始k现在如何引用commitEditingStyle的正确对象。这是来自另一个TableViewController的片段,它不使用节并且有一个引用我的managedObject的单个数组,因此我需要知道需要对此进行哪些更改以引用正确的对象,因为我将要处理indexPath .section and .row:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Ensure that if the user is editing a name field then the change is committed before deleting a row -- this ensures that changes are made to the correct event object.
[tableView endEditing:YES];
// Delete the managed object at the given index path.
NSManagedObject *flightConditionList = (self.flightConditionList)[indexPath.row];
[self.managedObjectContext deleteObject:flightConditionToDelete];
// Update the array and table view.
[self.flightConditionList removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// Commit the change.
NSError *error;
if (![self.managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
在此先感谢,如果它是如此简单,飞行员可以做到这一点。
这是一个编辑,试图去与第一个答案。不知道我是否正确地将实体分成AircraftTypes实体的部分,但我还需要根据aircraftTypes.type和.model命名节头。
尝试使用以下代码。不知道sectionNameKeyPath是否为飞机类型的关系实体编码。另外,我需要从airTypes实体类型和模型的两个属性标记节标题。
- (NSFetchedResultsController *) fetchedResultsController {
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Aircraft" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"tailNumber" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"aircraftType" cacheName:@"Master"];
self.fetchedResultsController = theController;
fetchedResultsController.delegate = self;
return fetchedResultsController;
}
请参阅上面的我的编辑代码(底部片段)来实现fetchedResultsController。我想我意识到我需要sectionNameKeyPath去aircraftTypes.type(NSString)。但是,我有多个相同“类型”的条目,因此是附加的“模型”标识符。你可以在多个关系字符串值上做一个sectionNameKeyPath吗?即'sectionNameKeyPath:@“aircraftType.type + aircraftType.model”'?? –