简体   繁体   English

使用节时如何编辑(即删除行)uiTableView?

[英]How do you edit(i.e. delete row) uiTableView when using sections?

Hello and Thanks to all. 您好,谢谢大家。 I have a table view where I allow the user to change to a sectioned view. 我有一个表格视图,允许用户更改为剖视图。 All is well in standard view however when the user switches to sectioned view I crash when I try to delete a row with the (-commitEditingStyle:) function. 在标准视图中一切都很好,但是当用户切换到剖视图时,当我尝试使用(-commitEditingStyle :)函数删除一行时,我崩溃了。 What am I missing in this function? 我在此功能中缺少什么? How should I delete a row when It's sectioned? 分段时应如何删除行? (I load my Table from a plist wchich is a dictionary of Arrays. Each index being a row in the tableview.) (我从plist加载我的Table,这是Arrays的字典。每个索引都是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.
    }   
         //////////////////////////////////
     }
}

CONSOLE: -[__NSCFString removeObjectAtIndex:]: unrecognized selector sent to instance 0xe4bd3f0 控制台:-[__ NSCFString removeObjectAtIndex:]:无法识别的选择器已发送到实例0xe4bd3f0

As you can see I have been toying with the format....not sure what to try next ??? 如您所见,我一直在尝试格式....不确定下一步该怎么做??? Thanks. 谢谢。

what I mean by sectioned: 我的意思是分段的: 在此处输入图片说明

So for one, you've got: 因此,您有:

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

where you are allocing mynewList and then immediately overwriting it. 您在其中分配mynewList,然后立即覆盖它。

But I think you should probably check the type of sectionRow in the debugger. 但是我认为您可能应该检查调试器中sectionRow的类型。 My guess is that it's an NSString. 我的猜测是它是一个NSString。

It's pretty hard to tell what's going on without knowing a bit more about your data structures. 在不了解您的数据结构的情况下,很难说出正在发生的事情。

Two problems are here. 这里有两个问题。 The below line will create a leak and should not be used in this way. 下一行将产生泄漏,因此不应以这种方式使用。

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

Instead of this you can put it as, 除了这个,你可以这样写,

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

Another thing is, 另一件事是

[mynewList objectForKey:@"Dis"]; is not always an array and it is sometimes coming as string. 并不总是一个数组,它有时以字符串形式出现。 Check your data structure and confirm if it is an array. 检查您的数据结构,并确认它是否为数组。

Try like this, 这样尝试

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

and check what is the data type of obj printed in NSLog . 并检查在NSLog打印的obj的数据类型是什么。 Confirm it is NSMutableArray and not NSString . 确认它是NSMutableArray而不是NSString

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我有多个iOS部分时,从UITableView中删除行(值在Dynamic NSMutableDictionary中) - Delete row from UITableView when I have multiple sections iOS (values are in Dynamic NSMutableDictionary) 如何从“联系人”应用程序复制红色的删除按钮? (即在分组的电视版式中,使其在选中时变暗) - How do I replicate the red delete button from the Contacts application? (i.e. in a grouped TV Layout and such that it darkens when selected) 如何在UITableView中划分部分? - How do I divide sections in UITableView? 当我有多个部分时,尝试从UITableView删除行时出现崩溃。 部分值在Dynamic NSMutableDictionary中 - Getting crash when try to delete row from UITableView when I have multiple sections. Section values are in Dynamic NSMutableDictionary 当我不知道会有多少节时,如何重新加载UITableView的数据? - How do I reload a UITableView's data when I don't know how many sections there will be? 如何从UITableView删除行? - How do you delete rows from UITableView? 在iOS 8.1中,当我在UITableView处于编辑模式时单击行的删除按钮时,该行及其上方的行显示不正确 - In iOS 8.1, when I click on a row's delete button while UITableView is in edit mode, the row and those above it display incorrectly 单击按钮时如何在uitableview中删除行 - how to delete row in uitableview when click on button 实现具有不同部分的UIScrollView(即Yahoo! Weather应用) - Implement a UIScrollView with different sections (i.e. Yahoo! Weather app) 如何使用当前设置将uitableview定制为不同的部分? - How do I customize a uitableview into different sections with my current setup?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM