菜鸟教程小白 发表于 2022-12-12 13:38:18

iphone - 排序的数组不会删除我想要的


                                            <p><p>我有一个排序好的 NSMutableArray,它工作得很好,尽管当我尝试删除一个对象时,它会使应用程序崩溃,然后当我重新加载应用程序时,它并没有删除正确的对象。</p>

<p>我知道这是因为它现在是一个排序数组,因为在我实现此功能之前它运行良好,尽管我不知道如何修复它。</p>

<p>这是我用来从数组中删除东西的代码:</p>

<pre><code>- (void) tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ( editingStyle == UITableViewCellEditingStyleDelete ) {
      Patient *thisPatient = ;
      ;
      if (patients.count == 0) {
            ;
            ;
      }
      ;
       withRowAnimation:UITableViewRowAnimationLeft];
    }
}
</code></pre>

<p>它正在这一行停止:</p>

<pre><code> withRowAnimation:UITableViewRowAnimationLeft];
</code></pre>

<p>这是我用于对数组进行排序的代码:</p>

<pre><code>-(void) processForTableView:(NSMutableArray *)items {

    for (Patient *thisPatient in items) {
      NSString *firstCharacter = ;
      if (!) {
            ;
             forKey:firstCharacter];
      } else {
             intValue] + 1] forKey:firstCharacter];
      }
    }
    charIndex = (NSMutableArray *) ;
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      UITableViewCell *cell = ;
      NSString *letter = ];
      NSPredicate *search = %@&#34;, letter];

    NSSortDescriptor *sortDescriptor = [ initWithKey:@&#34;patientName&#34; ascending:YES];

    NSArray *filteredArray = [ sortedArrayUsingDescriptors:];
    if ( nil == cell ) {
      cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@&#34;cell&#34;];
    }

    NSLog(@&#34;indexPath.row = %d, patients.count = %d&#34;, indexPath.row, patients.count);
    Patient *thisPatient = ];
    cell.textLabel.text = ;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.textColor = ;
    if (self.editing) {
      ;
    }
    return cell;
}
</code></pre>

<p>我怀疑这是在对数组进行排序时发生的很常见的事情,尽管它可能不是。</p>

<p>如果你想要更多的代码,请说</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这不一定是因为您的数组已排序,而是因为您从中填充表的数组与您从中删除对象的数组不同。</p>

<p>您在哪里对 <code>patients</code> 数组进行排序?是对实际的 <code>patients</code> 数组进行排序,还是在 tableView 委托(delegate)方法中对其进行排序,而不是实际对 <code>patients</code> 进行排序?</p>

<p>原因是您删除的对象的索引与实际 <code>patients</code> 数组中的索引不同(因为一个已排序,一个未排序)。正因为如此,它首先删除了错误的对象,然后它崩溃了,因为 tableView 期望删除一个对象(以便它可以动画化被删除的单元格)但错误的对象被删除了。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 排序的数组不会删除我想要的,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/18301590/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/18301590/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 排序的数组不会删除我想要的