2013-11-23 72 views
0

我得到這些錯誤的,當我嘗試使用刪除與我已張貼下面的代碼:如何用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]; 

} 
} 
+2

這看起來像你以前的問題的確切副本(爲此,你得到了一個對我來說很好的答案)。 –

+0

也許它是重複的。如何。當我嘗試使用給我的示例代碼之一時,出現錯誤 – KennyVB

+0

但是,從數據源數組*中移除行是正確的答案。如果這不適合你,那麼發佈一個關注這個問題的問題。 - (我敢打賭,你的數組不是*可變的,因此你不能刪除一個對象。) –

回答

1

它看起來像你的數據存儲在tableData,當你打電話deleteRowsAtIndexPaths,你是不是從tableData刪除相應的數據。如果您要在此表上調用reloadData,則數據不會真正消失,因爲您沒有從數據源中刪除它。我也同意馬丁R,你從數組中獲得的數據可能是不可變的。要創建你可以做類似的可變數據來源:

tableData = [[arrayA valueForKey:@"Hostname"] mutableCopy]; 

(確保資料表被聲明爲NSMutableArray的)

然後當你到commitEditingStyle,喜歡的東西刪除資料表中的數據:

[tableData removeObjectAtIndex:indexPath.row]; 

如果你允許刪除多行,你必須允許,也許通過使用removeObjectsInRange

+0

實際上工作。現在我只需要找到一個方法來寫它的plist。因爲現在當我重新加載應用程序。刪除被重新加載,就像它不寫plist一樣 – KennyVB