【问题标题】:get IndexPath in a Sorted Array获取有序数组中的 IndexPath
【发布时间】:2013-04-23 13:37:18
【问题描述】:

我正在获取从 plist 中排序的值并将它们显示在 tableview 中。我提供了输入将写入 plist 的自定义类别的功能。但我希望按字母顺序插入。有人可以建议我如何获取必须插入的行的indexpath。我使用了以下代码,自定义条目将插入到 0 位置

NSInteger section = 0;
NSInteger row = 0;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];

NSDictionary *categoryDictionary = [NSMutableDictionary dictionary];
[categoryDictionary setValue:newCategoryName forKey:@"name" ];
[categoryDictionary setValue:@"custom.png" forKey:@"image"];

[[self categoriesArray]insertObject:categoryDictionary atIndex:row];
[[self categoriesArray] writeToFile:[self dataFilePath] atomically:YES];

NSArray *indexPathsToInsert = [NSArray arrayWithObject:indexPath];
[[self tableView]insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationRight];
[self.categoriesArray writeToFile:[self dataFilePath] atomically:YES];

【问题讨论】:

    标签: ios objective-c uitableview nsarray plist


    【解决方案1】:

    试试

    NSMutableDictionary *categoryDictionary = [NSMutableDictionary dictionary];
    [categoryDictionary setObject:newCategoryName forKey:@"name" ];
    [categoryDictionary setObject:@"custom.png" forKey:@"image"];
    
    //Add new category to array
    [self.categoriesArray addObject:categoryDictionary];
    
    //Sort Array
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    [self.categoriesArray sortUsingDescriptors:@[sortDescriptor]];
    
    //Write array to plist
    [self.categoriesArray  writeToFile:[self dataFilePath] atomically:YES];
    
    NSInteger section = 0;
    //Get the index of saved Category item
    NSInteger row = [self.categoriesArray indexOfObject:categoryDictionary];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
    
    [self.tableView insertRowsAtIndexPaths:@[indexPath]
                           withRowAnimation:UITableViewRowAnimationRight];
    

    【讨论】:

    • *** 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[<__nscfstring> valueForUndefinedKey:]:该类不符合键名的键值编码。”跨度>
    • @ChandraSekhar 你能设置断点并告诉我代码在哪里中断。 self.categoriesArray 仅具有字典对象 rite,并且它们都有一个键“名称”,我假设这些进行了排序。也请检查一下
    • 错误出现在 sortUsingDescriptors 行。是的,它是一个字典数组,并且有一个键“名称”
    • @ChandraSekhar 你能在排序前提供分类数组的日志吗?
    • 我整理出来了。现在它正在工作,但自定义类别正在添加到最后一行
    【解决方案2】:

    在您的表格中,您将使用以下方法获得部分和行

    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    

    【讨论】:

      【解决方案3】:

      如果你有一个可排序的数组,你可以使用这个代码 sn-p 作为帮助:

          NSArray *sortedArray = @[@"a" , @"b", @"d", @"c", @"f"];
          sortedArray = [sortedArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
          NSLog(@"%@", sortedArray);
      
          int c = [sortedArray indexOfObject:@"c"];
          NSLog(@"%d", c);
      

      日志->

      (
          a,
          b,
          c,
          d,
          f
      )
      2
      

      所以你首先将你的对象插入到“toSort”数组中,然后获取它的索引;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多