0

您好,谢谢大家。我有一个表格视图,允许用户更改为剖视图。在标准视图中一切正常,但是当用户切换到剖视图时,当我尝试使用 (-commitEditingStyle:) 函数删除一行时,我崩溃了。我在这个功能中缺少什么?分段时我应该如何删除一行?(我从一个 plist 加载我的表,它是一个数组字典。每个索引都是 tableview 中的一行。)

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
     if ([strTitle isEqualToString:@"sectionView"]) {

         @try {


         if (editingStyle == UITableViewCellEditingStyleDelete) {
             // Delete the row from the data source.
           //  NSMutableDictionary *book =[[NSMutableDictionary alloc]init];
             NSMutableDictionary *mynewList = [[NSMutableDictionary alloc]init];
             mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
             NSMutableArray *sectionRow = [mynewList objectForKey:@"Dis"];


             [sectionRow removeObjectAtIndex:indexPath.row];

             [mynewList writeToFile:[self dataFilePath] atomically:YES];

             //
             [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

         } 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.
         }   
         }
         @catch (NSException *exception) {
             NSLog(@"hello error...%@",exception);
         }



     }

     else {
    /////////////////////// STANDARD VIEW 

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [distribArray removeObjectAtIndex:indexPath.row];
        //
        //        NSMutableArray *contents = [[NSMutableArray alloc] init];
        //        [contents addObject:myMasterList];
        //        //[contents addObject:[NSArray arrayWithObjects:arraydata.text,distroName.text,destorEmail.text, nil]];

        [distribArray writeToFile:[self dataFilePath] atomically:YES];

        //
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    } 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.
    }   
         //////////////////////////////////
     }
}

控制台:-[__NSCFString removeObjectAtIndex:]:无法识别的选择器发送到实例 0xe4bd3f0

如您所见,我一直在玩弄格式....不确定下一步该尝试什么???谢谢。

我所说的分段是什么意思:在此处输入图像描述

4

2 回答 2

0

因此,一方面,你有:

NSMutableDictionary *mynewList = [[NSMutableDictionary alloc]init];
mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

你在哪里分配 mynewList 然后立即覆盖它。

但我认为您可能应该在调试器中检查 sectionRow 的类型。我的猜测是它是一个 NSString。

如果不了解更多关于数据结构的信息,很难判断发生了什么。

于 2012-10-23T18:56:18.263 回答
0

这里有两个问题。下面的行会造成泄漏,不应以这种方式使用。

NSMutableDictionary *mynewList = [[NSMutableDictionary alloc]init];
             mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

而不是这个,你可以把它说成,

NSMutableDictionary *mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

另一件事是,

[mynewList objectForKey:@"Dis"];并不总是一个数组,它有时会以字符串的形式出现。检查您的数据结构并确认它是否是一个数组。

像这样试试

id obj = [mynewList objectForKey:@"Dis"];
NSLog(@"%@", [[obj class] description]);

并检查objprint in的数据类型是什么NSLog。确认它是NSMutableArray和不是NSString

于 2012-10-23T19:24:43.110 回答