【发布时间】:2014-07-07 08:19:04
【问题描述】:
我无法从我的 plist 文件中删除特定数据。我在stackoverflow中搜索了很多答案,但我无法解决我的问题。
这是我的代码:
在ViewDidLoad我从plist文件中检索数据。
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:@"manuallyData" ofType:@"plist"];
}
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.nameArr = [dict objectForKey:@"Name"]; // Here i got all my name
self.countryArr = [dict objectForKey:@"Country"]; // here i got all my country
}
我删除的代码:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)plistPath];
NSString *key = [self.nameArr objectAtIndex:indexPath.row];
[dictionary removeObjectForKey:[NSString stringWithFormat:@"%@", key]];
[dictionary writeToFile:plistPath atomically:YES];
self.nameArr = [dictionary allKeys];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
当我构建这个应用程序时,我得到了这个错误:
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 (2) must be equal to the number of rows contained in that section before the update (4), 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).'
请任何人建议我,我必须做什么?
感谢高级版。 :)
【问题讨论】:
-
您使用什么数据作为数据源?
self.nameArr?你能显示numberOfRowsInSection代码吗 -
你好,anil,我只使用你的代码,它显示错误。好的,我会分享,请帮助我
-
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.nameArr count]; }
-
self.nameArr 是一个 NSArray。我是属性合成这个
-
实际上您要删除什么?名字还是国家?
标签: ios