0

方法に困っていNSFetchedResultsController'sます。実装を読みましたが、クリックしていないようです。具体的には、これらの方法 (Apple ドキュメントから):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[<#Fetched results controller#> sections] count];
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[<#Fetched results controller#> sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[<#Fetched results controller#> sections] objectAtIndex:section];
    return [sectionInfo name];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [<#Fetched results controller#> sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [<#Fetched results controller#> sectionForSectionIndexTitle:title atIndex:index];
}

私が理解していないのはsectionssectionIndexTitlesプロパティです。を初期化するときにこれらのプロパティを宣言したことはありNSFetchedResultsControllerませんが、XCode はどのようにしてそれらを認識し、クラッシュせずに表示するのでしょうか?

さらにその方法では

NSFetchedResultsController *controller = [[NSFetchedResultsController alloc]
        initWithFetchRequest:fetchRequest
        managedObjectContext:context
        sectionNameKeyPath:nil
        cacheName:@"<#Cache name#>"]; 

の設定方法がわかりませんsectionNameKeyPath。たとえば、完了したタスクと未完了のタスクを含む To Do リストを作成したい場合、それらを 2 つのセクションに区別するにはどうすればよいでしょうか? タスク エンティティに値が必要ですか? そして、この値は文字列である必要がありますか/カスタムセッターを指定する必要がありますか?

助けていただければ幸いです。

4

1 に答える 1

1

1.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

これは、表に表示するセクションの数です。セクションとは、共通点のあるアイテムのグループです。彼らはすべて同じ文字で始まるかもしれません (連絡先アプリのように)、異なるリーグに所属するチームであるかもしれませんし、生まれた国によって分類された人々であるかもしれません.

2.

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;

これは、提供されたセクションに属するアイテムの数です。たとえば、フランス生まれの 3 人とドイツ生まれの 2 人がいるとします。したがって、取得した結果コントローラーに移動して、そのセクションにあるオブジェクトの数を尋ね、その数を返します。

3.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

これはセクションのタイトルです。「フランス」「ドイツ」など…

取得した結果コントローラで「sectionKeyPath」を指定した場合、section.name は、指定したパス名のデータ オブジェクトからグループ化された値になります。

つまり、sectionKeyPath = countryOfBirth.name;

これにより、そのセクションの各個人の出生地オブジェクトの国で名前の値が返されます。

4.

5.

これらは両方とも、テーブルの右端にあるインデックスに関連しています。それらはオプションです。他の方法を理解するまでは、これらを無視することをお勧めします。

したがって、私の例を使用すると、次のようにすることができます...

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestForEntityName:@"Person"];

NSSortDescriptor *countrySD = [NSSortDescriptor sortDescriptorWithKey:@"countryOfBirth.name" ascending:YES];
NSSortDescriptor *nameSD = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];

[fetchRequest setSortDescriptors:@[countrySD, nameSD]];

NSFetchedResultsController *controller = [[NSFetchedResultsController alloc]
    initWithFetchRequest:fetchRequest
    managedObjectContext:context
    sectionNameKeyPath:@"countryOfBirth.name"
    cacheName:nil];

これにより、次のようなものが得られます...

- Albania
    - Bob
    - Margaret
- France
    - Alice
    - John
- Zimbabwe
    - Jason
    - Zachary
于 2013-07-26T16:04:02.963 に答える