gpt4 book ai didi

iOS,分组 TableView ,按一个核心数据实体分组

转载 作者:行者123 更新时间:2023-11-29 12:23:30 25 4
gpt4 key购买 nike

My core Data

我有(顺序) TableView Controller ,按“代码”实体排序我想要做的是分组 TableView ,分组依据order.customer.name要这样

[customer one]
*order.code1 - product1
*order.code2 - product1
[customer two]
...

我在 viewWillAppear 中知道的是:

- (void)viewWillAppear:(BOOL)animated{


NSEntityDescription *orderDescription = [NSEntityDescription entityForName: @"Order"
inManagedObjectContext:self.context];
NSFetchRequest *request = [NSFetchRequest new];
[request setEntity:orderDescription];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"code" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];

self.controller = [[NSFetchedResultsController alloc]
initWithFetchRequest:request
managedObjectContext:self.context
sectionNameKeyPath:nil
cacheName:nil];


NSError *error;
[self.controller performFetch:&error];
}

这可能也很有用

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

// Configure the cell...
NSManagedObject *managedObject = [self.controller objectAtIndexPath:indexPath];
cell.textLabel.text = ((Order *) managedObject).code;
cell.detailTextLabel.text = ((Order *) managedObject).product.name;
return cell;
}

我找到了一些组 TableView 的解决方案,但大部分都不是针对核心数据的谢谢

最佳答案

这是你想要的吗?

- (void)setupFetch:(NSManagedObjectContext*)context {
NSEntityDescription *orderDescription = [NSEntityDescription entityForName: @"Order"
inManagedObjectContext:context];
NSFetchRequest *request = [NSFetchRequest new];
[request setEntity:orderDescription];

NSSortDescriptor *sortDescriptorSection = [[NSSortDescriptor alloc] initWithKey:@"customer.name" ascending:YES];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"code" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorSection, sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];

self.controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:context
sectionNameKeyPath:@"customer.name"
cacheName:nil];


NSError *error;
[self.controller performFetch:&error];
[self.tableView reloadData];
}

这应该根据客户名称创建组,但根据代码排序。

编辑:澄清。你想使用 sectionNameKeyPath 来实现你想要的。请注意,您需要像这样使用 NSFetchedResultsController 中的部分:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [self.controller.sections objectAtIndex:section];
return [sectionInfo numberOfObjects];
}

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

关于iOS,分组 TableView ,按一个核心数据实体分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29896284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com