Note that there are some explanatory texts on larger screens.

plurals
  1. POSorted arrays doesn't delete what I want
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>Here is the code I use to delete things from the array:</p> <pre><code>- (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]; } } </code></pre> <p>It is being stopped at this line:</p> <pre><code>[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; </code></pre> <p>Here is the code that I use for sorting the array:</p> <pre><code>-(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; } </code></pre> <p>I suspect this is quite a common thing that happens when you sort an array though it may not be.</p> <p>Please say if you want any more code</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload