CMSetController 0.1.0

CMSetController 0.1.0

测试已测试
语言语言 Obj-CObjective C
许可协议 MIT
发布时间最后发布2014年12月

Samuel Ford 维护。



  • 作者
  • Samuel Ford

CMSetController 是一个用于 NSSet 的 KVO 控制器,它在没有 Core Data 的情况下表现类似于 NSFetchedResultController。

给定一个对象,它有一个包含您希望绑定到表视图的对象的 NSSet 属性,提供(与 NSFetchedResultController 很相似)

  • 集合的键路径。
  • 要观察集合中对象的一个或多个键路径。
  • 一个或多个用于排序对象的排序描述符。
  • 可选地,将按对象的属性键路径分组。

控制器将提供一组部分和对象以填充 UITableView 数据源。当集合中的对象发生变化时,它还将调用回委托类以更新、插入、删除、重新排序和部分更改。

用法

在配置绑定项视图时配置 CMSetController。如果您没有包含集合的模型对象,请使集合成为您控制器的属性。

// watch for changes to either the "name" or "favoriteColor" properties
NSArray *paths = @[@"name", @"favoriteColor"];

// sort by "favoriteColor" (first sort must match section key path, if any) then "name"
NSArray *sorts = @[[NSSortDescriptor sortDescriptorWithKey:@"favoriteColor" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];

// observe the "friends" set property of the "self.document" object using the key paths and
// sorts setup above, group by "favoriteColor" into sections, and receive delegate callbacks
// as the set changes
_setController = [[CMSetController alloc] initWithObserved:self.document
                                                setKeyPath:@"friends"
                                                  keyPaths:paths
                                        sectionNameKeyPath:@"favoriteColor"
                                           sortDescriptors:sorts
                                                  delegate:self];

NSError *error;
if (![_setController performQuery:&error]) {
    NSLog(@"oops - %@", error);
    abort();
}

[self.tableView reloadData];

就像使用 NSFetchedResultController 一样填充表视图

#pragma mark - Table view

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    CMFriend *friend = [[[[_setController sections] objectAtIndex:indexPath.section] objects] objectAtIndex:indexPath.row];

    cell.textLabel.text = friend.name;
    cell.detailTextLabel.text = friend.favoriteColor;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[_setController sections] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[[_setController sections] objectAtIndex:section] name];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[_setController sections] objectAtIndex:section] numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

如果您实现了 CMSetControllerDelegate,您将获得针对更新表视图定制的回调

#pragma mark - Query Delegate

- (void)controllerWillChangeContent:(CMSetController *)controller
{
    [self.tableView beginUpdates];
}

- (void)controller:(CMSetController *)controller didChangeSection:(id<CMSetControllerSectionInfo>)sectionInfo atIndex:(NSUInteger)index forChangeType:(CMSetControllerChangeType)type
{
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];

    if (type == CMSetControllerChangeInsert)
        [self.tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    else
        [self.tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)controller:(CMSetController *)controller didChangeObject:(id)object atIndexPath:(NSIndexPath *)indexPath forChangeType:(CMSetControllerChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
    switch (type) {
        case CMSetControllerChangeInsert:
            [self.tableView insertRowsAtIndexPaths:@[newIndexPath]
                                  withRowAnimation:UITableViewRowAnimationAutomatic];
            break;

        case CMSetControllerChangeDelete:
            [self.tableView deleteRowsAtIndexPaths:@[indexPath]
                                  withRowAnimation:UITableViewRowAnimationAutomatic];
            break;

            case CMSetControllerChangeUpdate:
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                break;

        case CMSetControllerChangeMove:
            [self.tableView deleteRowsAtIndexPaths:@[indexPath]
                                  withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView insertRowsAtIndexPaths:@[newIndexPath]
                                  withRowAnimation:UITableViewRowAnimationFade];
            break;

        default:
            break;
    }
}

- (void)controllerDidChangeContent:(CMSetController *)controller
{
    [self.tableView endUpdates];
}

联系方式

Samuel Ford @causticmango

许可协议

CMSetController 在 MIT 许可下发布。有关更多详细信息,请参阅 LICENSE 文件。