2013-05-21 28 views
3

我有一個TableView與多個部分有一個委託數據源鏈接到數組數組。數組數組中的每個數組都是它自己的Table View Section。我已經正確加載並啓用了編輯按鈕,但是當我嘗試刪除我的應用程序崩潰時出現以下錯誤。XCODE TableView具有多個部分並從數組數組中委託數據源 - 無法刪除無錯誤的行?

***由於未捕獲異常'NSInternalInconsistencyException',原因:'無效更新:節數無效。更新後的表視圖中包含的段數(9)必須等於更新前表格視圖中包含的段數(10),加上或減去插入或刪除的段數(0插入,0刪除)。」

現在我不刪除一個部分,所以有點困惑,這裏是我的代碼示例。我已經廣泛搜索這個論壇,但只是無法找出我做錯了什麼。

這裏是我的代碼:

// Calculate how many sections in the table view from the array or arrays 

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ 

    AppDelegate *delegate = [[UIApplication sharedApplication]delegate]; 

    NSInteger sections = [delegate.packing.packingListArray count]; 

    return sections; 
} 

// Load the array and extract the count of items for each section 

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    // For each array within the array of array count the items 
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate]; 
    NSArray *sectionContents = [delegate.packing.packingListArray objectAtIndex:section]; 
    NSInteger rows = [sectionContents count]; 

    return rows; 
} 

// Load the list into the table view 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    // Set the cell identifier - using ID field thats been set in interface builder 
    static NSString *CellIdentifier = @"DestinationCell"; 

    // Re-use existing cell? 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    // If no reusabled cells then create a new one 
    if (cell == nil){ 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Add the relevant packing list array from the array of arrays 
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate]; 
    NSArray *sectionContents = [delegate.packing.packingListArray objectAtIndex:[indexPath section]]; 
    NSString *contentForThisRow = [sectionContents objectAtIndex:[indexPath row]]; 

    cell.textLabel.text = contentForThisRow; 



    // Return the formatted cell with the text it needs to display in the row 
    return cell; 
} 

// Delete row from table and update data source array 

- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(NSIndexPath *) indexPath { 

    if (editingStyle ==UITableViewCellEditingStyleDelete) { 


     [tableView beginUpdates]; 

     // Delete the item from the array 
     AppDelegate *delegate = [[UIApplication sharedApplication]delegate]; 
     [delegate.packing.packingListArray removeObjectAtIndex:indexPath.row]; 


     // Delete the row from the table view 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

     [tableView endUpdates]; 

    } 


} 

我有一種感覺,這是值得做的陣列創建多個節的陣列的也許是複雜但每畝可好歹這樣做的?

任何幫助非常感謝,因爲我現在一直在看這個相同的代碼幾個小時。

回答

3

我終於設法解決了這個問題,結果出現了一些問題。這一切都是由於我試圖從數組或數組中刪除項目的方式。我應該使用的代碼是這樣的:

AppDelegate *delegate = [[UIApplication sharedApplication]delegate]; 
[[delegate.packing.packingListArray objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row]; 

然後我曾與我有我的定義數組,因爲他們從中儘管設置的NSMutableArray他們被轉換成一個的NSArray內置的plist的方式的問題。所以在我的發言結束時,我包括在內。解決

........mutablecopy];

所有問題:)