当我尝试将删除与我在下面发布的代码一起使用时,我得到了这些错误:
*** 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 (6) must be equal to the number of rows contained in that section before the update (6), 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).'
我在按下编辑按钮时尝试使用删除功能。它表明我可以删除当我尝试显示上述错误时。
MainViewController.m
- (void)viewWillAppear:(BOOL)animated{
// Property List.plist code
//Gets paths from root direcory.
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
//Get documents path.
NSString *documentsPath = [paths objectAtIndex:0];
//Get the path to our PList file.
plistPath = [documentsPath stringByAppendingPathComponent:@"Servers.plist"];
//Check to see if Property List.plist exists in documents.
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]){
[[NSFileManager defaultManager]copyItemAtPath:[[NSBundle mainBundle]pathForResource:@"Servers" ofType:@"plist"] toPath:plistPath error:nil];
//If not in documents, get property list from main bundle.
}
arrayA = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
NSLog(@"%@\n%@", arrayA, [arrayA valueForKey:@"Hostname"]);
tableData = [arrayA valueForKey:@"Hostname"];
[super viewWillAppear:animated];
[self.mainTableView reloadData];
}
- (IBAction)setEditMode:(UIBarButtonItem *)sender {
if
(self.editing)
{
sender.title = @"Edit";
sender.style = UIBarButtonItemStylePlain;
[self.mainTableView setEditing:NO animated:YES];
[super setEditing:NO animated:YES];
}
else
{
sender.title = @"Done";
sender.style = UIBarButtonItemStyleDone;
[super setEditing:YES animated:YES];
[self.mainTableView setEditing:YES animated:YES];
}
}
- (NSInteger)tableView:(UITableView *)mainTableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"showServerAction";
UITableViewCell *cell = [mainTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)mainTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//- (void)tableView:(UITableView *)mainTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[arrayA writeToFile:plistPath atomically: TRUE];
[self.mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.mainTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
}
答案 0 :(得分:1)
您的数据似乎存储在tableData
中,当您致电deleteRowsAtIndexPaths
时,您不会从tableData
中删除相应的数据。如果您要在此表上调用reloadData,则数据实际上不会消失,因为您没有从数据源中删除它。我也同意Martin R的观点,你从数组中获得的数据可能并不可变。要创建可变数据源,您可以执行以下操作:
tableData = [[arrayA valueForKey:@"Hostname"] mutableCopy];
(确保tableData声明为NSMutableArray)
然后当你进入commitEditingStyle时,从tableData中删除数据,如:
[tableData removeObjectAtIndex:indexPath.row];
如果您允许删除多行,则必须允许这样做,可以使用removeObjectsInRange
。