TLCollapsibleTableViewController's
Core Data と統合するには、 defaultTLIndexPathController
を で初期化されたものに置き換えるだけで済みますNSFetchRequest
。
- (void)viewDidLoad
{
[super viewDidLoad];
// replace default indexPathController with one configured with an NSFetchRequest
NSManagedObjectContext *context = ...;// Your managed object context
NSFetchRequest *fetch = ...; // Request that fetches Scales sorted by category
self.indexPathController = [[TLIndexPathController alloc] initWithFetchRequest:fetch managedObjectContext:context sectionNameKeyPath:@"categories" identifierKeyPath:nil cacheName:nil];
// perform initial fetch
NSError *error;
[self.indexPathController performFetch:error];
if (error) {
// handle error
}
}
この回答の残りの部分では、Core Data 統合がどのように機能するかを説明し、TLIndexPathControllerDelegate
.
TLCollapsibleTableViewController
TLCollapsibleDataModels
をデータモデルとして使用します。TLCollapsibleDataModels
すべての可能な行と展開されたセクション名のセットを含むバッキング データ モデルを提供することによって構築されます。
TLCollapsibleDataModel *dataModel = [[TLCollapsibleDataModel alloc] initWithBackingDataModel:backingDataModel expandedSectionNames:expandedSectionNames];
結果のデータ モデルには、表示される行のみが含まれます (ただし、backingDataModel
プロパティを介して完全なデータ セットにアクセスできます)。ユーザーがセクションを展開または折りたたむとTLCollapsibleTableViewController
、データ モデルが自動的に更新されます。
上記のように Core Data 統合を有効にするTLIndexPathController
と、最初のフェッチ結果とフェッチ結果の変更時にデータ モデルも自動的に更新されます。ただし、このデータ モデルはバッキング モデルであるため、 に変換する必要がありますTLCollapsibleDataModel
。これを自動的に行うために、次の の実装を追加しましたcontroller:willUpdateDataModel:withDataModel:
。これにより、更新が生成される前に新しいデータ モデルを任意に変更できます。
- (TLIndexPathDataModel *)controller:(TLIndexPathController *)controller willUpdateDataModel:(TLIndexPathDataModel *)oldDataModel withDataModel:(TLIndexPathDataModel *)updatedDataModel
{
// If `updatedDataModel` is already a `TLCollapsibleDataModel`, we don't need to do anything.
if ([updatedDataModel isKindOfClass:[TLCollapsibleDataModel class]]) {
return nil;
}
// Otherwise, we assume `updatedDataModel` is the backing model - maybe it came from a
// Core Data fetch request or maybe it was provided by custom code - and we need to
// constructe the `TLCollapsibleDataModel`.
NSMutableSet *expandedSectionNames = nil;
// If `oldDataModel` is a `TLCollapsibleDataModel`, we need to preserve the
// expanded state of known sections.
if ([oldDataModel isKindOfClass:[TLCollapsibleDataModel class]]) {
expandedSectionNames = [NSMutableSet setWithSet:((TLCollapsibleDataModel *)oldDataModel).expandedSectionNames];
// Now filter out any section names that are no longer present in `updatedDataModel`
[expandedSectionNames intersectSet:[NSSet setWithArray:updatedDataModel.sectionNames]];
}
// Construct and return the `TLCollapsibleDataModel`
TLCollapsibleDataModel *dataModel = [[TLCollapsibleDataModel alloc] initWithBackingDataModel:updatedDataModel expandedSectionNames:expandedSectionNames];
return dataModel;
}
updatedDataModel
ではない場合はTLCollapsibleDataModel
、バッキング モデルと見なされて に変換されTLCollapsibleDataModel
、 でキャプチャされた展開状態が維持されoldDataModel
ます。これは、NSFetchRequest
生成された更新に対して機能するだけでなく、カスタム コードでバッキング モデルを設定することも、self.indexPathController.items = ...
何らかのself.indexPathController.dataModel = ...
理由でそうする必要がある場合にも機能します。