2012-07-06 52 views
0

我有一個按字母順序排列的tableview。我有一個NSMutableArray *drinks,這是我的原始數據源,它由plist填充NSMutableDictionary's。在我的viewDidLoad方法中。然後我通過遍歷數組,並創建了部分按鍵和各部分的行數在我viewWillAppear方法從項目被刪除後從分段tableview更新NSMutableArray

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    self.sections = [[NSMutableDictionary alloc] init]; 
    BOOL found; 

    // Loop through the whiskys and create our keys 
    for (NSMutableDictionary *whisky in self.drinks) 
    { 
     NSString *c = [[whisky objectForKey:NAME_KEY] substringToIndex:1]; 

     found = NO; 

     for (NSString *str in [self.sections allKeys]) 
     { 
      if ([str isEqualToString:c]) 
      { 
       found = YES; 
      } 
     } 

     if (!found) 
     { 
      [self.sections setValue:[[NSMutableArray alloc] init] forKey:c]; 
     } 
    } 
    // Loop again and sort the whiskys into their respective keys 
    for (NSDictionary *whisky in self.drinks) 
    { 
     [[self.sections objectForKey:[[whisky objectForKey:NAME_KEY] substringToIndex:1]] addObject:whisky]; 
    } 
    // Sort each section array 
    for (NSString *key in [self.sections allKeys]) 
    { 
     [[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:NAME_KEY ascending:YES]]]; 
    } 
     [self.tableView reloadData]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view 

    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [searchPaths lastObject]; 
    NSString *writeableDBPath = [documentsDirectory stringByAppendingPathComponent:@"ScotchList.plist"]; 

    NSMutableArray *tmpArray = [[NSMutableArray alloc]initWithContentsOfFile:writeableDBPath]; 

    self.drinks = tmpArray; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(applicationDidEnterBackground:) 
               name:UIApplicationDidEnterBackgroundNotification 
               object:nil]; 

    //Register for application exiting information so we can save data 
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil]; 

} 

所有這一切工作正常,並花花公子。然後當我刪除一行時,它也可以正常工作。如何當我去一個新的視圖,並返回到tableview再次出現刪除的行。所以我需要更新我的原始數據源(飲料),因爲每次viewAppears它再次從原始數據源拉。所以我試圖把這個物品(威士忌)放在行中,並將它從陣列中移除。但它拋出了一個sigabrt。這是我的刪除行方法。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //DELETE THE ROW FROM THE DATA SOURCE 
     [self.tableView beginUpdates]; 

     [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] removeObjectAtIndex:indexPath.row]; 

     NSDictionary *whisky = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; 
     [self.drinks removeObject:whisky]; 

     if ([[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] count] > 0) 
     { 
      // Section is not yet empty, so delete only the current row. 
      [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

     } 
     else 
     { 
      // Section is now completely empty, so delete the entire section. 
      [self.sections removeObjectForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]]; 
      [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] 
        withRowAnimation:UITableViewRowAnimationFade]; 
     } 

     [self.tableView endUpdates]; 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
    } 
} 

這是日誌顯示:

2012-07-06 15:36:42.454 WhiskyBook[3275:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]' 

*** First throw call stack: 

(0x3998a17b 0x3494395b 0x398d83bd 0x3e6b5 0x32a0cd11 0x328f0d09 0x328f0cbb 0x328f0c95 0x328f09eb 0x328f1369 0x328ef791 0x328dd72d 0x328dd009 0x339a7603 0x339a7233 0x3995a873 0x3995a817 0x39959611 0x398d6be5 0x398d6a71 0x339a634b 0x329037f5 0x3ac09 0x33cc7b20) 

libc++abi.dylib: terminate called throwing an exception 

(lldb) 

我在做什麼錯?

+0

這個叫什麼?你也許可以通過輸出你正在查看的數組並查看哪一個是零來解決這個問題。 – Dustin 2012-07-06 20:14:38

+0

此外,它看起來像你試圖改變一個不可變的數組 - 這就是爲什麼你的錯誤有__NSArrayI在它(我代表不可變) – Dustin 2012-07-06 20:19:29

+0

問題來了,當我把'NSDictionary *威士忌'內'tableview commitEditingStyle'方法。當我輸出威士忌到日誌時,它會按照它應該顯示的項目(威士忌)。 – 2012-07-06 20:38:07

回答

0

我找到了解決我的問題的方法。而不是試圖從commitEditingStyle方法的原始數據源中刪除對象,我只是爲我的master-viewController alloc'd創建了一個NSDictionary屬性,並在我的viewDidLoad中對其進行了插入,然後爲其指定了我想要刪除的項目commitEditingStyle方法。然後在我的viewWillDisappear方法中,我將它從原始數據源中刪除。

相關問題