質問

I have a sorted NSMutableArray which works perfectly and all though when I try to delete an object it crashes the app then when I reload the app it didn't delete the right one.

I known that is due to the fact that this is now a sorted array because before I implemented this feature it worked fine though I haven't got a clue of how to fix it.

Here is the code I use to delete things from the array:

- (void) tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)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];
    }
}

It is being stopped at this line:

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

Here is the code that I use for sorting the array:

-(void) processForTableView:(NSMutableArray *)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 sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)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;
}

I suspect this is quite a common thing that happens when you sort an array though it may not be.

Please say if you want any more code

役に立ちましたか?

解決

It's not neccessarily because your array is sorted, but because the array you are populating your table from is not the same as the array that you are removing the object from.

Where are you sorting the patients array? Is the actual patients array being sorted or are you sorting it in your tableView delegates method and not actually sorting patients?

The reason for this is that the index of the object you deleted is not the same as the index that it has in the actual patients array (because one is sorted and one is not). Because of this, it is first deleting the wrong object, then it crashes because the tableView expects one to be deleted (so that it can animate that cell being removed) but the wrong one was deleted.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top