TLIndexPathTools与核心数据集成以实现可扩展的tableView [英] TLIndexPathTools integration with core data for expandable tableView

查看:99
本文介绍了TLIndexPathTools与核心数据集成以实现可扩展的tableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的预加载的SQLite DB和Core Data模型,其实体名为"Scales",其中包括NSString名称"和NSString字段"categories"(我决定不使用关系,因为这是一个简单的一对一,没有大量的数据.

I have a simple pre-loaded SQLite DB and Core Data model with an Entity called "Scales" which includes a NSString "name" and a NSString field called "categories" (I decided not to use a relationship since this was a simple one-to-one without a large amt of data).

使用核心数据的当前设计如下:类别[a至z]的UITableViewController->单击类别-> DetailTableViewController,显示属于该类别"的所有名称" [a至z].

Current design is as follows using core data: UITableViewController of category [a to z] ->click on category -> DetailTableViewController displaying of all "names" [a to z] which fall under that "category".

我能够使用标准的Apple提取方式并使用谓词进行排序以完成向下钻取的任务,但是它确实需要向堆栈中添加另一个视图控制器.

I am able to accomplish these tasks of drilling down using the standard Apple way of fetching and using a predicate to sort, however it does require adding another view controller to the stack.

我的目标是能够使用可扩展的UItableview,其中节名称为类别",单元格为名称";从而无需将详细视图推送到堆栈上/使应用在美学上更加令人愉悦.

My goal is to be able to use an expandable UItableview where the section names are the "categories", and the cells are the "names"; thereby making it unnecessary to push a detail view onto the stack/make the app more aesthetically pleasing.

我试图将Tim Moose的TLIndexPathTools中的CollapseTableViewController与我的项目集成在一起,但是我仍然不清楚细节……最具体地说,是在哪里链接我的ManagedObjectcontext和获取说明符,以便他的工具可以完成其余工作

I’ve attempted to integrate the CollapseTableViewController from Tim Moose’s TLIndexPathTools, with my project, but I am still unclear with the details….most specifically, where to link up my managedObjectcontext and fetch specifiers so that his tools can do the rest.

推荐答案

要与Core Data集成,您只需要将TLCollapsibleTableViewController's默认TLIndexPathController替换为用NSFetchRequest初始化的一个:

To integrate with Core Data, you should only need to replace TLCollapsibleTableViewController's default TLIndexPathController with one initialized with an 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的一个很酷的功能.

The rest of this answer explains how the Core Data integration works and highlights a cool feature of TLIndexPathControllerDelegate.

TLCollapsibleTableViewController使用TLCollapsibleDataModels作为数据模型. TLCollapsibleDataModels通过提供包含所有可能的行和一组扩展的节名称的后备数据模型来构造:

TLCollapsibleTableViewController uses TLCollapsibleDataModels as the data model. TLCollapsibleDataModels are constructed by providing a backing data model containing all possible rows and a set of expanded section names:

TLCollapsibleDataModel *dataModel = [[TLCollapsibleDataModel alloc] initWithBackingDataModel:backingDataModel expandedSectionNames:expandedSectionNames];

结果数据模型仅包含要显示的行(尽管可以通过backingDataModel属性访问完整的数据集). TLCollapsibleTableViewController的作用是在用户展开或折叠部分时自动更新数据模型.

The resulting data model only contains the rows to be displayed (though the full data set is accessible through the backingDataModel property). What TLCollapsibleTableViewController does is to automatically update the data model when the user expands or collapses a section.

如上所述,当您启用Core Data集成时,TLIndexPathController还将根据初始获取结果和任何获取结果更改自动更新数据模型.但是此数据模型是支持模型,需要转换为TLCollapsibleDataModel.为了使这种情况自动发生,我添加了controller:willUpdateDataModel:withDataModel:的以下实现,该实现允许人们在生成更新之前任意修改新的数据模型:

When you enable Core Data integration as described above, the TLIndexPathController will also automatically update the data model on the initial fetch result and on any fetch result changes. But this data model is the backing model and needs to be converted to TLCollapsibleDataModel. To make this happen automatically, I added the following implementation of controller:willUpdateDataModel:withDataModel:, which allows one to arbitrarily modify the new data model before the updates are generated:

- (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 = ...中设置一个支持模型.

So if updatedDataModel isn't a TLCollapsibleDataModel, it is assumed to be the backing model and is converted to TLCollapsibleDataModel, preserving the expanded state captured in oldDataModel. This will not only work for NSFetchRequest generated updates but you can also set a backing model in your custom code self.indexPathController.items = ... or self.indexPathController.dataModel = ... if you have any reason to do so.

这篇关于TLIndexPathTools与核心数据集成以实现可扩展的tableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆