gpt4 book ai didi

ios - 具有核心数据和非核心数据源的 UITableView

转载 作者:行者123 更新时间:2023-11-29 10:44:50 27 4
gpt4 key购买 nike

我的 iOS Core Data 应用程序存在一些 TableView 问题。在应用程序中有一个 Master View Controller,它包含一个 TableView 和一个单独的 Creator View Controller,它使用填充的数据创建 Core Data 托管对象。

该应用程序的当前目标是将核心数据对象中的文本加载到表格 View 单元格(在 MVC 表格 View 中),并且当点击该单元格时,在下方(下拉)显示一个新单元格,显示附加信息信息。

我在 -[tableView:cellForRowAtIndexPath:] 中遇到问题,我尝试使用 [self.fetchedResultsController objectAtIndexPath:indexPath]; 访问特定的核心数据对象> 由于试图实例化额外的子单元格,我无法在正确的索引路径上调用正确的对象。如果我有足够的父细胞,我会收到一个不正确的对象作为返回。如果我没有足够的父单元格,我会因为调用索引范围外的对象而出现 SIGABRT 错误。

我有一个数组,其中包含子单元格的所有索引路径,因此在 -[tableView:numberOfRowsInSection:]

中实现了行计数

如果有人有任何建议,或者我需要澄清,请告诉我。我不想做一个困惑的代码转储,所以我展示了我认为是 MasterViewController.h 文件的适用部分。 (如果我太多了,还请提醒我)

@interface MasterViewController ()
@property (nonatomic, strong) NSMutableArray *subIndexPaths;
@end

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

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Trinomial" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSInteger count = [_managedObjectContext countForFetchRequest:fetchRequest error:&error];

if ([self.subIndexPaths count] == 0 || !self.subIndexPaths) {
return count;
}

return (count + [self.subIndexPaths count]);

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Child cell created & returned here
{
...
}

//Create a parent cell & setup/return
UITableViewCell *parentCell = [tableView dequeueReusableCellWithIdentifier:kParentCell];
Trinomial *currentTri = [self.fetchedResultsController objectAtIndexPath:indexPath];
parentCell.textLabel.text = [currentTri retrieveTrinomial];
return parentCell;

}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[[tableView cellForRowAtIndexPath:indexPath] reuseIdentifier] isEqualToString:kParentCell]) {
if ([self.subIndexPaths containsObject:[NSIndexPath indexPathForRow:(indexPath.row + 1) inSection:indexPath.section]]) {
//When there is already a child cell present

[self.subIndexPaths removeObject:newIndexPath];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView reloadData];

} else {
//When there isn't a child cell present

[self.subIndexPaths addObject:newIndexPath];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView reloadData];

}

最佳答案

好的,如果您继续,您当前尝试做的事情很快就会变得非常困惑。

相反,我会更改您的方法,以便任何没有子单元格的父单元格显示为当前单元格类型,而任何有子单元格的父单元格显示为不同的单元格类型 - 其中包括 parent 和 child 的内容

这样,您的索引就不会一团糟。您的节数和行数变得简单。 (顺便说一句,您不应该运行新的提取来获取计数,而是从 FRC 获取它)。

唯一有点复杂的是确定行高和要使用的单元格类型...

关于ios - 具有核心数据和非核心数据源的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22734528/

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