简体   繁体   English

具有多个NSFetchedResultsControllers的UITableView导致断言失败

[英]UITableView with multiple NSFetchedResultsControllers causing Assertion failure

I have a UITableView with 3 sections and each section is being fed through unique NSFetchedResultsController. 我有一个包含3个部分的UITableView,每个部分都通过唯一的NSFetchedResultsController进行馈送。

I am getting Assertion failure from the NSFetchedResultsController -controllerDidChangeContent , when inserting, updating...the table. 插入,更新...表时,我从NSFetchedResultsController -controllerDidChangeContent获取断言失败

My guess is issue with indexPaths coming in method below as every controller has only single section (0) and for controller in section 0 the failure doesn't happen. 我的猜测是以下方法中出现indexPaths的问题,因为每个控制器只有一个(0)部分,而对于第0部分中的控制器,不会发生故障。

- (void)controller:(NSFetchedResultsController*)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath*)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath*)newIndexPath
{
    switch(type)
    {
        case NSFetchedResultsChangeInsert:
            [[self atableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
            break;
        case NSFetchedResultsChangeDelete:
            [[self atableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
            break;
        case NSFetchedResultsChangeUpdate:
            [self configureCell:(DashboardViewCell *) [[self atableView] cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;
        case NSFetchedResultsChangeMove:
            [[self atableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [[self atableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    } 
}

So my question is, how can I determine which controller (from which section) is being processed and modify the indexPath accordingly and if that's the right way of doing it? 所以我的问题是,如何确定正在处理哪个控制器(从哪个部分开始)并相应地修改indexPath?这是否是正确的方法? And possibly any examples of using several nsfetchedresultscontrollers with one uitableview. 以及在一个uitable视图中使用多个nsfetchedresultsscontrollers的任何示例。

So my guess was actually correct: 所以我的猜测实际上是正确的:

as each controller enters following function 当每个控制器进入以下功能时

  • (void)controller:(NSFetchedResultsController*)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath*)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath*)newIndexPath (void)控制器:(NSFetchedResultsController *)控制器didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath

the indexPath is incorrect for every controller apart from the one that is setup for section 0. 除了为第0节设置的控制器以外,每个控制器的indexPath都是错误的。

Each controller has only single section - 0 in my case but more importantly, these sections do not correspond to the table sections. 每个控制器只有一个部分-在我的情况下为0,但更重要的是,这些部分与表部分不对应。

So the workaround I currently implemented (not great, so will probably rework) is to check for cacheName of the controller and based on that modify the section for indexPath/newIndexPath 因此,我当前实现的解决方法(不太好,所以可能会重做)是检查控制器的cacheName并基于此修改indexPath / newIndexPath的部分

something like this: 像这样的东西:

if([[controller cacheName] isEqualToString:@"sectionX"])
{
  indexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:<add correct section>];
  newIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:<add correct section>];
}

I am not sure it is the FetchedResultController. 我不确定这是FetchedResultController。

The insert and delete row call will at some point ask the tableView to reload, it will then ask the delegate and datasource methods for numberOfRowsInSection, cellForRowAtIndexPath etc. 插入和删除行调用有时会要求tableView重新加载,然后它将向委托和数据源方法询问numberOfRowsInSection,cellForRowAtIndexPath等。

What probably happens is that the model and the tableView are out of sync and it causes the controller to throw a warning. 可能发生的情况是模型和tableView不同步,并导致控制器发出警告。

    [tableView beginUpdates];

     if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSMutableDictionary *item = [self.itemList objectAtIndex:indexPath.row];
    // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self.itemList removeObjectAtIndex:indexPath.row];      

}   
    [tableView endUpdates];

Try something like this, where all changes to the tableView and the underlying model are wrapped in the beginUpdates and endUpdates. 尝试类似的方法,其中对tableView和基础模型的所有更改都包装在beginUpdates和endUpdates中。 Those will cause the tableView to wait with the drawing of the cell until you give the 'OK'. 这些将导致tableView等待单元格的绘制,直到您单击“确定”为止。

If the above is not the case here is how I usually deal with multiple sections in a tableView. 如果不是上述情况,这就是我通常在tableView中处理多个部分的方式。

In your header you declare a typedef enum for the sections; 在标头中,为各节声明一个typedef枚举;

typedef enum {

    SectionTypeName,
    SectionTypeAge,
    SectionTypeSkills,

} SectionType;

//in the implementation

switch (indexPath.section) {
    case SectionTypeName:
        //do thing in the name section
        break;
    case SectionTypeAge:
        //do thing in the name section
        break;
    case SectionTypeSkills:
        //do thing in the name section
        break;          
    default:
        break;
}

I have the switch() in almost every tableView delegate/datasource method. 我几乎在每个tableView委托/数据源方法中都有switch()。 This makes it very easy to figure out which section is being processed and what it does. 这样可以很容易地弄清楚正在处理的部分及其功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 断言失败 - [UITableView _endCellAnimationsWithContext: - Assertion failure in -[UITableView _endCellAnimationsWithContext: 断言失败 - [UITableView _configureCellForDisplay:forIndexPath:], - Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], 断言失败 - [UITableView _configureCellForDisplay:forIndexPath:] - Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:] 在iOS 7上-[UITableView layoutSublayersOfLayer:]中的断言失败 - Assertion failure in -[UITableView layoutSublayersOfLayer:] on iOS 7 断言失败 UITableView:删除与另一个对象有关系的对象 - Assertion failure UITableView: delete object which a relationship to another object ***选择“后退”按钮时,-[UITableView _endCellAnimationsWithContext:]中的断言失败 - *** Assertion failure in -[UITableView _endCellAnimationsWithContext:] when selecting the back button 尝试从UITableView删除行时断言失败 - Assertion failure when trying to delete a row from UITableView 奇怪的UITableView错误:将故事板中的静态单元出队时出现“断言失败” - Strange UITableView error: “Assertion Failure” on dequeuing static cells in storyboard -[NSLayoutConstraint常量]中的断言失败 - Assertion failure in -[NSLayoutConstraint constant] UITextView _firstBaselineOffsetFromTop中的断言失败 - Assertion failure in UITextView _firstBaselineOffsetFromTop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM