代码之家  ›  专栏  ›  技术社区  ›  maddie

在调用“DeleteRowstIndepaths:withRowAnimation”之前删除对象仍会生成:无效更新:第0节中的行数无效

  •  3
  • maddie  · 技术社区  · 7 年前

    问题是,用户在调用之前忽略了从数据数组中删除对象 deleteRowsAtIndexPaths:withRowAnimation reloadData 然后 deleteRowsAtIndexPaths:withRowAnimation .

    但是,我确实从数据源中删除了该对象(a) NSFetchedResultsController )通话前 deleteRowsAtIndexPaths:withRowAnimation . 我不打电话 .

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        /*Only allow deletion for collection table */
        if(_segmentedControl.selectedSegmentIndex == 1) {
            NSLog(@"delete ca");
            if (editingStyle == UITableViewCellEditingStyleDelete)
        {
            CollectedLeaf* collectedLeaf = [collectionFetchedResultsController objectAtIndexPath:indexPath];
            LeafletPhotoUploader * leafletPhotoUploader = [[LeafletPhotoUploader alloc] init];
            leafletPhotoUploader.collectedLeaf = collectedLeaf;
    
            if([LeafletUserRegistration isUserRegistered]) {
                [leafletPhotoUploader deleteCollectedLeaf:collectedLeaf delegate:self];
            }
    
    
            // Delete the managed object for the given index path
            NSManagedObjectContext *context = [collectionFetchedResultsController managedObjectContext];
            [context deleteObject:[collectionFetchedResultsController objectAtIndexPath:indexPath]];
    
            // Save the context.
            NSError *error;
            if (![context save:&error])
            {
                NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
                NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
                if(detailedErrors != nil && [detailedErrors count] > 0)
                {
                    for(NSError* detailedError in detailedErrors)
                    {
                        NSLog(@"  DetailedError: %@", [detailedError userInfo]);
                    }
                }
                else 
                {
                    NSLog(@"  %@", [error userInfo]);
                }
            }
    
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    
        }
    
    }
    

    无效更新:节0中的行数无效。数量 更新(2)后现有节中包含的行必须为 更新(2),加上或减去从中插入或删除的行数 该部分(插入0,删除1)加上或减去 移入或移出该节的行(0移入,0移出)。

    而不是 .

    enter image description here

    虽然它解决了错误,但表单元格并没有真正被删除,您可以通过仍然存在的“Collected:null”标签看到: enter image description here

    此viewcontroller有一个分段控件,其索引更改加载到表中的数据。数据从 NSFetchedResultsController

    if(_segmentedControl.selectedSegmentIndex == 1) {
           /// [_table removeFromSuperview];
            _search_bar.hidden=YES;
    
            UIImage *btnImage2 = [UIImage imageNamed:seg2_buttonImg];
            [_left_button setImage:btnImage2 forState:UIControlStateNormal];
            NSError *error;
            [self.collectionFetchedResultsController performFetch:&error];
            [self collectionFetchedResultsController];
            collectedLeafArray = [collectionFetchedResultsController fetchedObjects];
            [_table reloadData];
        }
    

    因此,以下是我对numberOfRowsInSection的实现:

    - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        if(_segmentedControl.selectedSegmentIndex == 0){
            id <NSFetchedResultsSectionInfo> sectionInfo = [[speciesFetchedResultsController sections] objectAtIndex:section];
    
            return [sectionInfo numberOfObjects];
        }
        id <NSFetchedResultsSectionInfo> sectionInfo = [[collectionFetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo numberOfObjects];
    }
    

    删除行后,如何更新collectionFetchedResultsController的计数?

    1 回复  |  直到 7 年前
        1
  •  0
  •   maddie    7 年前

    I found a solution here . 基本上我是在打电话 deleteRowsAtIndexPaths:withRowAnimation 在数据源有时间完成删除之前。

    对于其他正在努力修改UITableView的人,这里有一个委托方法来处理它,并 here is the Apple documentation it was taken from

    - (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;
    
            case NSFetchedResultsChangeDelete:
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                       withRowAnimation:UITableViewRowAnimationFade];
                break;
    
            case NSFetchedResultsChangeUpdate:
                [self configureCell:[tableView cellForRowAtIndexPath:indexPath]
                    atIndexPath:indexPath];
                break;
    
            case NSFetchedResultsChangeMove:
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                       withRowAnimation:UITableViewRowAnimationFade];
                [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                       withRowAnimation:UITableViewRowAnimationFade];
                break;
        }
    }