- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
11-24-2013:我进行了更多调试,发现 removeProject 工作正常。 (我已经打印了删除前后的所有项目)只有当它返回 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 时,计数才为 0 而不是(total - 1)。
============
我现在被这个错误困住了一段时间。为了更好地理解 Core Data,我创建了这个测试项目,您可以在其中输入客户及其项目。项目与客户相关。对于项目,我复制了 Clients 的 ViewController 并做了一些小改动。我可以输入几个客户,然后删除它们。当我想删除相关项目时,问题就开始了。如果有一个客户只有一个项目,我可以毫无错误地删除它。如果客户有两个或更多项目,我无法从该客户中删除任何项目。删除会给我这个错误:
2013-10-30 10:00:23.145 [6160:70b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:1330
2013-10-30 10:00:23.147 [6160:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) 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 (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:
(
.....
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Program ended with exit code: 0
在这个网站上阅读了很多答案后,我的猜测是我需要更改我的代码 numberOfRowsInSection 方法,但我看不出要在那里更改什么。
我的代码:
我已经创建了一个数据存储:
数据存储.m
#import "BITDataStore.h"
#import "Client.h"
#import "Project.h"
@implementation DataStore
+ (BITDataStore *)sharedStore
{
static BITDataStore *sharedStore = nil;
if (!sharedStore)
sharedStore = [[super allocWithZone:nil]init];
return sharedStore;
}
-(void)removeClient:(Client *)client
{
// remove from NSManagedObjectContext
[context deleteObject:client];
// remove from allClients array
[allClients removeObjectIdenticalTo:client];
NSLog(@"remove client");
}
-(void)removeProject:(Project *)project
{
// remove from NSManagedObjectContext
[context deleteObject:project];
// remove from allProjects array
[allProjects removeObjectIdenticalTo:project];
// remove from relatedProjects array
[relatedProjects removeObjectIdenticalTo:project];
NSLog(@"remove project %@", [project project]);
}
-(NSArray *)relatedProjects:(Client *)client;
{
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:@"Project"];
[request setEntity:e];
// Check if client is related to Project
[request setPredicate: [NSPredicate predicateWithFormat:@"clients == %@", client.objectID]];
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"project"
ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sd]];
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
if (!result) {
[NSException raise:@"Fetch failed"
format:@"Reason: %@", [error localizedDescription]];
}
relatedProjects = [[NSMutableArray alloc] initWithArray:result];
return relatedProjects;
}
@end
ProjectDataViewController.m
@synthesize client, project;
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[self tableView] reloadData];
}
#pragma mark - Table view data source
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[BITDataStore sharedStore]relatedProjects:client]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil){
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
Project *p = [[[BITDataStore sharedStore]relatedProjects:client]
objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[p project]];
return cell;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
BITDataStore *ds = [BITDataStore sharedStore];
NSArray *selectedProjects = [ds relatedProjects:client];
Project *pr = [selectedProjects objectAtIndex:[indexPath row]];
NSLog(@"Deleting project %@", [pr project]);
[ds removeProject:pr];
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Finally, reload data in view
[[self tableView] reloadData];
NSLog(@"Reload data");
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
@end
希望这些信息足够了,如果不够请告诉我。
最佳答案
将我的删除规则从级联更改为无效,它起作用了。
关于ios - 删除行 : error 'Invalid update: invalid number of rows in section 0' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19678559/
从 angular 5.1 更新到 6.1 后,我开始从我的代码中收到一些错误,如下所示: Error: ngc compilation failed: components/forms/utils.
我正在学习 Typescript 并尝试了解类型和接口(interface)的最佳实践。我正在玩一个使用 GPS 坐标的示例,想知道一种方法是否比另一种更好。 let gps1 : number[];
type padding = [number, number, number, number] interface IPaddingProps { defaultValue?: padding
这两种格式在内存中保存结果的顺序上有什么区别吗? number = number + 10; number += 10; 我记得一种格式会立即保存结果,因此下一行代码可以使用新值,而对于另一种格式,
在 Python 匹配模式中,如何匹配像 1 这样的文字数字在按数字反向引用后 \1 ? 我尝试了 \g用于此目的的替换模式中可用的语法,但它在我的匹配模式中不起作用。 我有一个更大的问题,我想使用一
我的源文件here包含 HTML 代码,我想将电话号码更改为可在我的应用程序中单击。我正在寻找一个正则表达式来转换字符串 >numbernumber(\d+)$1numbernumber<",我们在S
我们有一个包含 2 个字段和一个按钮的表单。我们想要点击按钮来输出位于 int A 和 int B 之间的随机整数(比如 3、5 或 33)? (不需要使用 jQuery 或类似的东西) 最佳答案 你
我收到以下类型错误(TypeScript - 3.7.5)。 error TS2345: Argument of type '(priority1: number, priority2: number
只想创建简单的填充器以在其他功能中使用它: function fillLine(row, column, length, bgcolor) { var sheet = SpreadsheetApp
我有一个问题。当我保存程序输出的 *.txt 时,我得到以下信息:0.021111111111111112a118d0 以及更多的东西。 问题是: 这个数字中的“d0”和“a”是什么意思? 我不知道“
首先:抱歉标题太长了,但我发现很难用一句话来解释这个问题;)。是的,我也四处搜索(这里和谷歌),但找不到合适的答案。 所以,问题是这样的: 数字 1-15 将像这样放在金字塔中(由数组表示):
我想从字符串中提取血压。数据可能如下所示: text <- c("at 10.00 seated 132/69", "99/49", "176/109", "10.12 I 128/51, II 1
当尝试执行一个简单的 bash 脚本以将前面带有 0 的数字递增 1 时,原始数字被错误地解释。 #!/bin/bash number=0026 echo $number echo $((number
我有一个类型为 [number, number] 的字段,TypeScript 编译器(strict 设置为 true)出现问题,提示初始值值(value)。我尝试了以下方法: public shee
你能帮我表达数组吗:["232","2323","233"] 我试试这个:/^\[("\d{1,7}")|(,"\d{1,7}")\]$/ 但是这个表达式不能正常工作。 我使用 ruby(rail
这个问题在这里已经有了答案: meaning of (number) & (-number) (4 个回答) 关闭6年前. 例如: int get(int i) { int res = 0;
我正在考虑使用 Berkeley DB作为高度并发的移动应用程序后端的一部分。对于我的应用程序,使用 Queue对于他们的记录级别锁定将是理想的。但是,如标题中所述,我需要查询和更新概念建模的数据,如
我正在尝试解决涉及重复数字的特定 JavaScript 练习,为此我需要将重复数字处理到大量小数位。 目前我正在使用: function divide(numerator, denominator){
我有这个数组类型: interface Details { Name: string; URL: string; Year: number; } interface AppState {
我们正在使用 Spring 3.x.x 和 Quartz 2.x.x 实现 Web 应用程序。 Web 服务器是 Tomcat 7.x.x。我们有 3 台服务器。 Quartz 是集群式的,因此所有这
我是一名优秀的程序员,十分优秀!