OGeek|极客世界-中国程序员成长平台

标题: iphone - 排序的数组不会删除我想要的 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:38
标题: iphone - 排序的数组不会删除我想要的

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

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

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

- (void) tableViewUITableView *)tv commitEditingStyleUITableViewCellEditingStyle)editingStyle forRowAtIndexPathNSIndexPath *)indexPath {
    if ( editingStyle == UITableViewCellEditingStyleDelete ) {
        Patient *thisPatient = [patients objectAtIndex:indexPath.row];
        [patients removeObjectAtIndex:indexPath.row];
        if (patients.count == 0) {
            [super setEditing:NO animated:YES];
            [self.tableView setEditing:NO animated:YES];
        }
        [self deletePatientFromDatabase:thisPatient.patientid];
        [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

它正在这一行停止:

[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

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

-(void) processForTableViewNSMutableArray *)items {

    for (Patient *thisPatient in items) {
        NSString *firstCharacter = [thisPatient.patientName substringToIndex:1];
        if (![charIndex containsObject:firstCharacter]) {
            [charIndex addObject:firstCharacter];
            [charCount setObject:[NSNumber numberWithInt:1] forKey:firstCharacter];
        } else {
            [charCount setObject:[NSNumber numberWithInt:[[charCount objectForKey:firstCharacter] intValue] + 1] forKey:firstCharacter];
        }
    }
    charIndex = (NSMutableArray *) [charIndex sortedArrayUsingSelectorselector(localizedCaseInsensitiveCompare];
}
- (UITableViewCell *)tableViewUITableView *)tv cellForRowAtIndexPathNSIndexPath *)indexPath {
        UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier"cell"];
        NSString *letter = [charIndex objectAtIndex:[indexPath section]];
        NSPredicate *search = [NSPredicate predicateWithFormat"patientName beginswith[cd] %@", letter];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey"patientName" ascending:YES];

    NSArray *filteredArray = [[patients filteredArrayUsingPredicate:search] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    if ( nil == cell ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier"cell"];
    }

    NSLog(@"indexPath.row = %d, patients.count = %d", indexPath.row, patients.count);
    Patient *thisPatient = [filteredArray objectAtIndex:[indexPath row]];
    cell.textLabel.text = [NSString stringWithFormat"%@ %@", thisPatient.patientName, thisPatient.patientSurname];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.textColor = [UIColor blackColor];
    if (self.editing) {
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    return cell;
}

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

如果你想要更多的代码,请说



Best Answer-推荐答案


这不一定是因为您的数组已排序,而是因为您从中填充表的数组与您从中删除对象的数组不同。

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

原因是您删除的对象的索引与实际 patients 数组中的索引不同(因为一个已排序,一个未排序)。正因为如此,它首先删除了错误的对象,然后它崩溃了,因为 tableView 期望删除一个对象(以便它可以动画化被删除的单元格)但错误的对象被删除了。

关于iphone - 排序的数组不会删除我想要的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18301590/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4