0

これは、私の以前の質問に対するフォローアップの質問です。

コア データ: 多対多の関係で従業員契約を管理していますか?

その質問には図があり、簡単なリマインダーとして次のとおりです。

company --< contracts >-- employees

各エンティティ内に 1 つのエンティティを手動で保存し、それらすべてを NSLog で確認できました。

すべての会社を一覧表示する CompanyListPage を作成しました。アイデアは、会社をクリックすると、その会社と契約しているすべての従業員のリストが表示されるということです。

コンテキストとして以下を参照してください。

Company: 
name: A

Contract:
length: 33 wks
salary: 20000

Employee:
name: Bob Jones

CompanyListPage 内の didSelectRowAtIndex ページには、次のものがあります。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    Company *c = [fetchedResultsController objectAtIndexPath:indexPath];    
    NSLog(@"You clicked %@", c.name);
    NSString *companyName = c.name;

    EmployeesListPage *employeesListPage = [[EmployeesListPage alloc] initWithNibName:@"EmployeesListPage" bundle:[NSBundle mainBundle]];

    [self.navigationController pushViewController:employeesListPage animated:YES];

    employeesListPage.title = companyName;  
    employeesListPage.managedObjectContext = self.context;
    employeesListPage.managedObject = c;

    [superstarsList release];

}

ただし、問題は、最終的に employeesListPage に移動したときに NSPredicate がどのように見えるかがわからないことです。

現時点では、これは次のとおりです。

- (NSFetchedResultsController *)fetchedResultsController 
{

    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }

    // Create and configure a fetch request
    NSFetchRequest *fetchRequest    = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity     = [NSEntityDescription entityForName:@"Employees" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    // Create the sort descriptors array
    NSSortDescriptor *authorDescriptor  = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:authorDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // Create and initialize the fetch results controller
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    self.fetchedResultsController = aFetchedResultsController;
    fetchedResultsController.delegate = self;

    // Memory management.
    [aFetchedResultsController release];
    [fetchRequest release];
    [authorDescriptor release];
    [sortDescriptors release];

    return fetchedResultsController;
}    

明らかにこれは間違っています。

a) 契約実体を調べること b) 会社実体を何らかの方法、形、形式で使用すること

NSPredicate を使用する必要があることはわかっていますが、「契約期間が 0 を超え、会社 A で働いているすべての従業員を見つけてください」と言う方法を知っているだけです。最初に最小の契約期間でそれを。

これに関する指針やヘルプは素晴らしいでしょう。ありがとうございました。


編集:最初の試み(以下の回答に従って動作するようになったため削除されました)

編集: 契約情報を取得できませんか?

会社 A で働くすべての従業員をテーブル ビュー コントローラーに戻すことができました。

ただし、従業員とその契約期間/給与に関する情報をセルに表示したいと考えています。

私は次のことを試しました:

Employee *emp = [fetchedResultsController objectAtIndexPath:indexPath];


NSString *firstname = emp.firstname;
NSString *surname = emp.surname;

NSString *fullname = [firstname stringByAppendingString:@" "];
fullname = [fullname stringByAppendingString:surname];

// Logging tests
NSLog(@"Name: %@", fullname); // This is fine
NSLog(@"Contracts: %@", emp.empContracts);  // This tells me of a problem with "Relationship fault for <NSRelationshipDescription: 0x602fdd0>"

コントラクト データだけでなく、すべてのコントラクト データを取得するには、NSPredicate を取得する必要があると思います。しかし、私は間違っているかもしれません。

繰り返しますが、これに関する助けをいただければ幸いです。

4

3 に答える 3

1

ANY キーワードを使用すると、方程式の左側が単一の量に復元され、方程式の右側と一致すると思います。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@“ANY contracts.employer ==  %@“, employer];
于 2011-03-23T20:23:47.353 に答える
0

私は今、答えを持っていると思います。

テーブルビューで次のことを行い、有効な関係とすべての出力を取得しました。ただし、for ループが含まれているため、正しく実行しているかどうかはわかりません。

ソリューションには、次の StackOverflow 関連の質問/回答を使用しました。

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{
    Employee *emp = [fetchedResultsController objectAtIndexPath:indexPath];

    NSString *firstname = emp.firstname;
    NSString *surname   = emp.surname;

    NSString *fullname  = [firstname stringByAppendingString:@" "];
    fullname            = [fullname stringByAppendingString:surname];

    NSLog(@"Employee: %@", fullname);

    // Output the contract deals.
    for (Contracts *deals in emp.contracts) 
    {

        NSNumber *cLength   = deals.length;
        NSNumber *cSalary   = deals.salary;

        NSLog(@"Length: %@", cLength);
        NSLog(@"Salary: %@", cSalary);
    } // next
}

私にとっての次のステップは、これをカスタム ラベル付きのカスタム ビューに含めて、このすべての情報を保持することです。

for ループがこれを行うための最も効率的な方法でない場合は、提案/改善、またはベスト プラクティスと見なされるものを歓迎します。

ありがとうございました。

于 2011-03-24T18:12:49.500 に答える
0

順調に進んでいるようです。そこから必要なのは、セルのテキストを設定することだけだと思います: cell.textLabel = fullname.

しかし、なぜあなたが契約関係を掘り下げているのか、私にはよくわかりません — 一番上のあなたのサンプル テキストを見るまでは。そこでは、あなたが望むのは雇用主の契約のリストであり、契約ごとに、その対1の従業員関係とその他の属性を表示したいようです。その場合、新しいフェッチはまったく必要ありません。すでに雇用主があり、その対多契約関係を通じて、契約エンティティもあり、それらを通じて、期間、給与、従業員などがあります。

雇用者テーブルで選択を変更すると、次のようになります。

NSUInteger row = [indexPath row];
Employer *employer = [self.arrayOfEmployersUsedToPopulateTable objectAtIndex:row];
self.selectedEmployer = employer;
self.arrayOfSelectedEmployersContracts = [employer.contracts allObjects]; // which probably faults them, but I think that’s OK here
// Then make a call to reload the other table, the one presenting the contracts info.

契約テーブルでは、選択した雇用主を参照し、各契約の情報を提示します。

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
Contract *contract = [self.arrayOfSelectedEmployersContracts objectAtIndex:row];
NSUInteger column = [indexPath column];
switch (column) {
    (0) :
    cell.textLabel = contract.length; // maybe you have to use an NSNumber method to convert to string, but probably not
    break;
    (1):
    cell.textLabel = contract.employee.firstname;
    break;
    // and so forth
}}

PS UITableCell については、Mark と LaMarche による Beginning iPhone Development の「Using the New Table View Cell」を参考にしました。この本も好きかも。

于 2011-03-24T20:11:20.563 に答える