我得到這些錯誤的,當我嘗試使用刪除與我已張貼下面的代碼:如何用plist從uitableview中刪除?
*** 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];
}
}
這看起來像你以前的問題的確切副本(爲此,你得到了一個對我來說很好的答案)。 –
也許它是重複的。如何。當我嘗試使用給我的示例代碼之一時,出現錯誤 – KennyVB
但是,從數據源數組*中移除行是正確的答案。如果這不適合你,那麼發佈一個關注這個問題的問題。 - (我敢打賭,你的數組不是*可變的,因此你不能刪除一個對象。) –