【问题标题】:NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'NSInternalInconsistencyException', reason: '试图将第 0 行插入第 0 节,但更新后第 0 节只有 0 行'
【发布时间】:2012-07-29 02:47:38
【问题描述】:

我正在使用 UITableViewController 并在更新 tableView 时收到此错误。以下是我的代码:

当我执行点击事件时会发生这种情况:

[timeZoneNames insertObject:@"HELLO" atIndex:0];  
[self.tableView beginUpdates];   
NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];

我尝试寻找苹果文档,但没有帮助。

谢谢, 阿比

【问题讨论】:

    标签: objective-c ios uitableview


    【解决方案1】:

    我忘记正确设置数据源出口时遇到了这个错误。

    如果您看到此错误,请检查您是否已明确设置 TableView 委托和数据源:

    • 转到您的界面构建器并查看带有“表格视图”的视图

    • cmd + 右键单击​​拖动(您应该会看到一条蓝线)从“表格视图”图标到文件的标题图标,在 xcode 6 中它旁边有一个黄色图标。

    • 松开鼠标,您应该会看到委托和数据源选项。

    • 选择“数据源”

    您的桌子现在应该已正确接线。

    【讨论】:

    • 这种方式对我来说是正确的。只需要在数据源和表之间进行连线!!!!。在委托和表之间!!!。不是视图控制器。但是有表。
    • 这就是为我做的。我在 View Controller 中声明了一个表,所以我忘了设置数据源,Xcode 没有抱怨,所以我从不看它,一直专注于实际数据。
    • 这对我帮助很大。我的问题是我有一个插座(someTableView),但我在 self.tableView 而不是 self.someTableView 上调用了方法
    【解决方案2】:

    我以前遇到过这个问题。这意味着当-insertRowsAtIndexPaths:withRowAnimation:被调用时-tableView:numberOfRowsInSection:返回0。你需要在调用-insertRowsAtIndexPaths:withRowAnimation:之前插入模型(见:-beginUpdates


    更新

    不知道-tableView:numberOfRowsInSection:的返回值是多少?另外,如果您只有一个更新,则不需要-beginUpdates/-endUpdates

    [timeZoneNames insertObject:@"HELLO" atIndex:0];
    // Let's see what the tableView claims is the the number of rows.
    NSLog(@"numberOfRowsInSection: %d", [self tableView:self.tableView numberOfRowsInSection:0]);
    NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
    [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop];
    

    【讨论】:

    • 我已经这样做了.. [timeZoneNames insertObject:@"HELLO" atIndex:0];行数总是大于 1。我仍然得到这个错误..
    • numberOfRowsInSection 的返回类型为 [timeZoneNames count];在没有 beginUpdates 和 endUpdates 的情况下使用 insertRowsAtIndexPaths 可以吗??
    • @Aby 来自-beginUpdates 文档:“如果您希望后续的插入、删除和选择操作(例如,cellForRowAtIndexPath: 和 indexPathsForVisibleRows)同时进行动画处理,请调用此方法。”它用于将多个 UI 操作组合成一个 UI 操作:您只有一个操作。
    【解决方案3】:

    我尝试在更新期间打印出 tableView 中实际有多少行和部分,这让我开始思考,我在 table view 数据源方法中返回了多少部分......这是罪魁祸首:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 0;
    }
    

    确保返回 1 而不是 0。

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    

    至少这为我解决了问题...

    更新:

    运行一段时间后,我又遇到了同样的问题。我稍微修改了应用程序。我的第一个视图是 tableView,当您单击右上角的加号按钮时,您会看到另一个可以输入信息的表。当您点击完成时,信息将使用以下代码添加到 Core Data 模型中:

    - (IBAction)doneButtonPressed:(UIBarButtonItem *)sender
    {
    
    MoneyBadgeAppDelegate *appDelegate =
    [[UIApplication sharedApplication] delegate];
    
    NSManagedObjectContext *context =
    [appDelegate managedObjectContext];
    NSManagedObject *newMoment;
    newMoment = [NSEntityDescription
                 insertNewObjectForEntityForName:@"SpendingMoment"
                 inManagedObjectContext:context];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyyMMdd"];
    NSDate *modifiedDate = [dateFormat dateFromString: self.dateTextField.text];
    
    [newMoment setValue: modifiedDate forKey:@"date"];
    [newMoment setValue: descTextField.text forKey:@"desc"];
    
    [appDelegate.eventsArray insertObject:newMoment atIndex:0];
    
    NSError *error;
    [context save:&error];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    }
    

    我确认它已成功放入核心数据模型,方法是在返回到第一个屏幕时在我的 viewDidAppear 方法中向控制台打印以下内容。

    -(void)viewDidAppear:(BOOL)animated{
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SpendingMoment"  
    inManagedObjectContext:self.managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];
    
    NSError *error;
    NSArray *items = [self.managedObjectContext
                      executeFetchRequest:fetchRequest error:&error];
    
    for (SpendingMoment *moment in items) {
        NSLog(@"Date: %@, Desc: %@", [moment date], [moment desc]);
    }
    
    [self addEvent];
    

    然后我的 addEvent 方法会这样做:

    - (void)addEvent {
    
    [self.tableScreen beginUpdates];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableScreen insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                          withRowAnimation:UITableViewRowAnimationFade];
    
    
    [self.tableScreen reloadData];
    [self.tableScreen endUpdates];
    
    @try{
        [self.tableScreen scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
    @catch(NSException *exception){
    }
    

    }

    知道这次是什么问题吗???谢谢!

    【讨论】:

    • 您永远不应该将reloadData 放在beginUpdates 块中。这两件事是相互排斥的
    【解决方案4】:

    勾选此“尝试将第0行插入第0节,但更新后第0节只有0行”。

    当您在第 0 行第 0 部分插入对象时,它会出现错误,没有任何部分。 尝试在插入行之前插入节。

     [timeZoneNames insertObject:@"HELLO" atIndex:0];
     NSLog(@"sections: %lu ",[self.downlaodTableView numberOfSections];
     // insert section 
     [self.tableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)] withRowAnimation:UITableViewRowAnimationNone];
     NSLog(@"numberOfRowsInSection: %d", [self tableView:self.tableView  numberOfRowsInSection:0]);
     NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
     [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop];
    

    【讨论】:

      猜你喜欢
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      相关资源
      最近更新 更多