gpt4 book ai didi

ios - 在NSFetchedResultsController中标识数组的数组

转载 作者:行者123 更新时间:2023-12-01 18:19:36 25 4
gpt4 key购买 nike

我已经从一本书中找到的教程中构建了一些代码。它可以工作,并且我能够在tableView中成功显示CoreData中的数据。现在,我想确定fetchRequest返回的数据/对象。我感觉像是个假人,因为我无法充分理解语法来隔离包含数据数组的对象。这是我难以理解的代码片段:

    - (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sessions" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
//NSLog(@"Entity is set to: %@", entity);
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"refid" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

//[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;

return _fetchedResultsController;

}

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

}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Sessions *info = [_fetchedResultsController objectAtIndexPath:indexPath];

//Format cell data ready to be displayed
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE, dd LLL yyyy"];
NSString *dateString = [dateFormat stringFromDate:info.date];

NSNumber *dist1Nbr = info.dist1;
int dist1Int = [dist1Nbr integerValue];
float distIntKorM = ([dist1Nbr integerValue])/1000;
NSString *dist1StrMeters = [[NSString alloc] initWithFormat:@"%i", dist1Int];
NSString *dist1StrKorM = [[NSString alloc] initWithFormat:@"%.01f", distIntKorM];


//Select image to display
if ([info.sport isEqualToString:@"Run"]) {
UIImage *image = [UIImage imageNamed:@"trainers-15x10.png"];
cell.imageView.image = image;
cell.textLabel.text = [[NSString alloc] initWithFormat:@"%@: (%@),", dateString, info.sport];
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"Type: %@, Dist: %@", info.sessiontype, dist1StrKorM];
} else if ([info.sport isEqualToString:@"Other"]) {
UIImage *image = [UIImage imageNamed:@"weights-15x10.png"];
cell.imageView.image = image;
cell.textLabel.text = [[NSString alloc] initWithFormat:@"%@: (%@),", dateString, info.sport];
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"Type: %@, Dist: %@", info.sessiontype, dist1StrKorM];
}

}

如果有人可以帮助我,将不胜感激。

最佳答案

您可以通过调用以下命令来访问NSFetchedResultsSectionInfo对象数组:

NSArray *sections = _fetchedResultsController.sections;

这些对象中的每一个都代表表中的一个部分。对于给定的部分,您可以执行以下操作:
id<NSFetchedResultsSectionInfo>section = sections[0];
NSArray *objects = section.objects;

访问该部分中的托管对象。或者,如果您有索引路径,则可以通过执行以下操作直接访问关联的对象:
NSManagedObject *object = [_fetchedResultsController objectAtIndexPath:indexPath];

反向开启:
NSIndexPath *indexPath = [_fetchedResultsController indexPathForObject:object];

这是您要找的东西吗?

更新

忘了这个。正如Juan在下面建议的那样,您可以使用以下命令访问所有提取的对象:
NSArray *allObjects = _fetchedResultsController.fetchedObjects;

关于ios - 在NSFetchedResultsController中标识数组的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18451774/

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