2013-03-16 59 views
0

删除UITableView部分的最后一行时出现此错误。ios6删除导致错误的部分中的最后一个单元格

***由于未捕获异常'NSInternalInconsistencyException',原因:'无效更新:节数无效。更新后的表视图中包含的段数(2)必须等于更新前的表视图中包含的段数(3),加上或减去插入或删除的段数(0插入,0删除)。”

我的表格有3个部分如果一个数组的计数大于0(用户可以自己添加位置)。如果数组等于0,那么该表只包含2个部分,这是我认为问题所在的地方,我无法弄清楚。

我有删除的细胞和更新数组的代码是:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell* delCell = [tableView cellForRowAtIndexPath:indexPath]; 
    delName = delCell.textLabel.text; 
    [self deleteLocation]; 
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
} 

-(void)deleteLocation { 


    NSLog(@"Name = %@",delName); 

    NSUserDefaults *customLocations = [NSUserDefaults standardUserDefaults]; 
    NSUserDefaults *sharedPref = [NSUserDefaults standardUserDefaults]; 


    [customLocations removeObjectForKey:delName]; 
    [customLocations synchronize]; 

    [customLoc removeObject:delName]; 
    saveCustomLoc = [NSArray arrayWithArray:customLoc]; 

    [sharedPref setObject:saveCustomLoc forKey:@"CustomLocations"]; 
    [sharedPref synchronize]; 

    NSLog(@"%@",saveCustomLoc); 
    NSLog(@"%lu",(unsigned long)[saveCustomLoc count]); 

    [self showAlert]; 



} 

- (void) showAlert { 

    UIAlertView *alert = [[UIAlertView alloc] 

          initWithTitle:@"Location Deleted" 
          message:@"The location has been deleted" 
          delegate:nil 
          cancelButtonTitle:@"Ok" 
          otherButtonTitles:nil]; 

    [alert show]; 

} 

,直到最终细胞的伟大工程。

的我的表是如何设置的一个例子是:

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

{ 
    if ([customLoc count] == 0) { // If there is no data in the custom locations array the don't include that section 
     if (section == 0) { 
      return [serverSelection count]; 

     } 
     else { 
      return [qServerSelection count]; 

     } 
    } 
    else { 
     if (section == 0) { 
      return [customLoc count]; 

     } 
     else if (section == 1) { 
      return [serverSelection count]; 

     } 
     else { 
      return [qServerSelection count]; 
     } 
    } 

所以标题/行数/段等的数目是依赖于一个阵列的内容。事实上,细胞正在从消失中删除(或者突然有4个细胞在第1部分移动到第0部分),我认为是造成这个问题。

任何想法如何解决这个问题?

回答

0

在调查了一些后,我发现(在一些苹果论坛成员的帮助下),当从一节中删除最后一行时,还需要显式删除该节。

我也在开始和结束时加入表更新代码,如下所示:

所以,如果我的阵列是0(在效果中的最后一行被删除)本身被去除部分。

+0

您可以拥有0行的部分。你的问题是由你自己的代码不一致造成的。如果你的'commitEditingStyle'方法简单地从表(和数据源)删除最后一行,那么你的'numberOfRowsInSection'也必须返回0。但是如果有0行,你的'numberOfSections'和'numberOfRowsInSection'就会忽略整个部分。这是造成不一致性错误的原因。 – rmaddy 2013-03-17 21:51:43

+0

感谢澄清@rmaddy! – 2013-03-18 18:24:18

相关问题