将核心数据表视图拆分为多个部分

时间:2014-05-26 14:18:19

标签: ios objective-c uitableview

目前我的表格视图加载了很长的项目列表NSMutableArray *certificates; 我想根据certificateType将这些组分成几个部分。到目前为止,我已对这些部分进行了分组,但需要根据certificateType

将数据添加到各个部分

从核心数据对象UITableView

填充Certificate

我可以使用cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", thisCert.certificateType.title, thisCert.title];

的名称将这些内容分类

或者我应该将NSMutableArray *certificates;分成4个独立的数组

任何关于最佳方法的建议都将受到赞赏

#pragma mark - Table view data source

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

    return 4;
}

//Section Titles
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

    //Section Text
    [[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont systemFontOfSize:18]];

    //Section Titles
    NSArray *sectionTitles = [NSArray arrayWithObjects:@"BlueCert", @"GreenCert", @"RedCert", @"OrangeCert", nil];
    NSString *result = [sectionTitles objectAtIndex:section];

    return result;


}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0f;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (tableView == self.searchDisplayController.searchResultsTableView) {

        return self.searchResults.count;
    }
    else {

        return self.certificates.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LogCmd();
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0f];
        cell.imageView.image = [UIImage imageNamed:@"IconFolderOpen"];
    }


    Certificate *thisCert;

    if (tableView == self.searchDisplayController.searchResultsTableView) {

        thisCert = (self.searchResults)[indexPath.row];
    }
    else {

        thisCert = (self.certificates)[indexPath.row];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", thisCert.certificateType.title, thisCert.title];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Reference: %@", thisCert.reference];

    return cell;
}

1 个答案:

答案 0 :(得分:0)

如果您的数据是动态的,我建议您设置NSFetchedResultsController。

这样,您的数据仍然在托管对象上下文中,但是表视图的数据源可以根据标题分解为多个部分。将sectionNameKeyPath设置为NSFetchedResultsController的证书标题。根据用于将数据导入NSFetchedResultsController的NSFetchRequest的证书标题设置排序描述符。

将视图控制器实现为NSFetchedResultsController的委托。

添加一些高级代码以进行详细说明。大部分代码都来自Marcus Zarra的Ios,OS X和Icloud核心数据存储和管理一书。 假设您有一个托管对象上下文,并且您正在视图控制器中添加NSFetchedResultsController和NSFetchRequest的属性

    - (NSFetchedResultsController *) certificatesFetchedResultsController{
    if(!_certificatesFetchedResultsController){

        _certificatesFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:self.certificatesFetchRequest managedObjectContext:self.certitificatesMOC sectionNameKeyPath:@"title" cacheName:nil];
        _certificatesFetchedResultsController.delegate = self;
    }
    return _certificatesFetchedResultsController;
}

- (NSFetchRequest *) certificatesFetchRequest{
    if(!_certificatesFetchRequest){
        _certificatesFetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Certificate"];
        NSSortDescriptor *sortTitle = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:NO];
        [_certificatesFetchRequest setSortDescriptors:@[sortTitle]];
    }
    return _certificatesFetchRequest;
}


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

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

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type{

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [[self transactionsTblView] insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            [[self transactionsTblView] deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }

}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{
    NSArray *newArray = @[[NSIndexPath indexPathForItem:newIndexPath.row inSection:newIndexPath.section];
    NSArray *oldArray = @[[NSIndexPath indexPathForItem:indexPath.row inSection:indexPath.section];

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.transactionsTblView insertRowsAtIndexPaths:newArray withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            [self.transactionsTblView deleteRowsAtIndexPaths:oldArray withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    NSManagedObject *object = [self.certificatesFetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
    // Create your section title using data in the managed object and return the UIView

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   NSManagedObject *object = [self.certificatesFetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
    // Configure cell data as per data in managed object
}

在某处,初始化获取的结果控制器。也许在viewDidLoad

[self.certificatesFetchedResultsController performFetch:nil];