gpt4 book ai didi

iphone - 具有两个 NSFetchedResultsController 实例的 TableView

转载 作者:可可西里 更新时间:2023-11-01 03:11:58 25 4
gpt4 key购买 nike

经过几天的研究和重新编码,我几乎被难住了。我的目标是让一个测试应用程序运行,其中包含一个由两个单独的 fetchedResultControllers 填充的 TableView 。

我在购物 list 上有一系列商品,每个商品都有一个部门和一个 bool 值“已收集”标志。未收集的项目应按部门列出,然后是包含所有已收集项目(无论部门)的单个部分。当用户勾选未收集的项目时,他们应该向下移动到“已收集”部分。如果他/她取消选中收集的项目,则应将其移回正确的部门。

enter image description here

为了实现第一部分(未收集的项目),我设置了一个简单的 fetchedResultsController,它获取 collected = NO 的所有项目,并按部门划分结果:

- (NSFetchedResultsController *)firstFRC {
// Set up the fetched results controller if needed.
if (firstFRC == nil) {

// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

// fetch items
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

// only if uncollected
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(collected = NO)"];
[fetchRequest setPredicate:predicate];

// sort by name
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

// fetch results, sectioned by department
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"department" cacheName:nil];
aFetchedResultsController.delegate = self;
self.firstFRC = aFetchedResultsController;
}

return firstFRC;
}

我按如下方式设置行数、节数和节标题:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return firstFRC.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [[firstFRC.sections objectAtIndex:section] numberOfObjects];
}

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

我还使用样板 controllerWillChangeContent、didChangeObject 和 controllerDidChangeContent 在 FRC 发生变化时添加/删除单元格。

为简洁起见,我不会包括用于显示单元格的代码,但我基本上是根据单元格的索引路径提取正确的项目,设置单元格的文本/副标题,并附上两个复选标记图像之一,具体取决于关于元素是否被收集。我还连接了此图像(位于按钮上),以便在触摸时从选中切换为未选中,并相应地更新项目。

这部分一切正常。我可以按部门查看我的项目列表,当我将其中一项标记为已收集时,我会看到它按预期从列表中删除。

现在我尝试添加底部部分,其中包含所有收集的项目(在一个部分中)。首先,我设置了第二个 fetchedResultsConroller,这次只获取未收集的项目,并且没有分段。我还必须更新以下内容:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections - add one for 'collected' section
return firstFRC.sections.count + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section
if (section < firstFRC.sections.count) {
return [[firstFRC.sections objectAtIndex:section] numberOfObjects];
}
else {
return secondFRC.fetchedObjects.count;
}
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section < firstFRC.sections.count) {
return [[[firstFRC sections] objectAtIndex:section] name];
}
else{
return @"Collected";
}
}

然后我以类似的方式更新了 cellForRowAtIndexPath,以便我检索的项目来自正确的 FRC:

    Item *item;
if (indexPath.section < firstFRC.sections.count) {
item = [firstFRC objectAtIndexPath:indexPath];
}
else {
item = [secondFRC objectAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
}

[[cell textLabel] setText:[item name]];

…rest of cell configuration

这在我启动时效果很好。 tableview 完全按照预期显示:

enter image description here

问题(最终)

当我为未收集的项目选择复选标记时,(第一个)问题出现了。我希望该项目从它所列的部门中删除,并移至“已收集”部分。相反,我得到:

CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)

如果我尝试相反的操作,则会收到不同的错误:

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'

我怀疑在这两种情况下,当一项从一个 FRC 移动到另一个 FRC 时,FRC 和 TableView 中的部分/行数存在一致性问题。尽管第二个错误让我觉得可能存在与我的元素检索相关的更简单的问题。

任何方向或想法将不胜感激。如果有帮助,我可以提供更多我的代码,并且还创建了一个带有单一 View 的小型测试应用程序来演示该问题。如果需要,我可以上传它,但主要是我想在一个小规模的沙箱中测试这个问题。

更新 - 请求附加代码

根据要求,这是触摸复选标记时发生的情况:

- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
Item *item;
if (indexPath.section < firstFRC.sections.count) {
item = [firstFRC objectAtIndexPath:indexPath];
}
else {
item = [secondFRC objectAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
}

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)cell.accessoryView;

if (![item collected]) {
[item setCollected:YES];
[button setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
}
else if ([item collected]){
[item setCollected:NO];
[button setBackgroundImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
}
NSError *error = nil;
if (![item.managedObjectContext save:&error]) {
NSLog(@"Error saving collected items");
}
}

最佳答案

好吧,我想我可能已经破解了它。通常情况下,将其剥离并阅读一些评论就为我指明了正确的方向。

当我到达委托(delegate)方法 controllerDidChangeObject 时,我尝试在提供的 indexPath 中插入一行(对于“选中”项目)。除了插入到我的附加部分时,此 indexPath 没有意识到在它之前还有许多其他部分这一事实。所以它接收第 0 节并尝试插入那里。相反,如果 indexPath 来自第二个 FRC,我应该将节号增加第一个 FRC 表中的节数。所以,我更换了:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 

UITableView *tableView = self.tableView;

switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

break;

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

UITableView *tableView = self.tableView;

switch(type) {
case NSFetchedResultsChangeInsert:

if ([controller.sectionNameKeyPath isEqualToString:@"department"]) {
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else {
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:newIndexPath.row inSection:firstFRC.sections.count]] withRowAnimation:UITableViewRowAnimationFade]; }
break;

我需要为每个插入、删除、更新等操作执行此操作。我将在验证后将其标记为答案,并留出时间发表其他评论。

关于iphone - 具有两个 NSFetchedResultsController 实例的 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8997387/

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