2013-08-18 2 views
2

완벽하게 작동하는 정렬 된 NSMutableArray가 있습니다.하지만 객체를 삭제하려고 시도 할 때 응용 프로그램이 손상되고 응용 프로그램을 다시로드하면 올바른 것을 삭제하지 못합니다.정렬 된 배열은 내가 원하는 것을 지우지 않습니다

이 기능은 이전에 정렬 된 배열이기 때문에이 기능을 구현하기 전에 제대로 작동했기 때문에 그 사실을 알았습니다. 문제를 해결하는 방법에 대한 단서가 없었기 때문입니다. 여기

내가 배열에서 사물을 삭제하는 데 사용하는 코드입니다 :

- (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]; 
    } 
} 
그것은이 라인에서 중단되고

: 여기

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

은 내가 배열을 정렬하는 데 사용하는 코드 :

-(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; 
} 

나는 어레이를 정렬 할 때 매우 일반적이라고 생각한다. . 당신이에서 테이블을 채우기하는 배열은 당신이에서 개체를 제거하는 배열과 같은하지 않기 때문에 당신이 당신의 배열이 정렬되어 있기 때문에 그것은 neccessarily 아니라 더 이상 코드

답변

1

를 원하지만, 경우

은 말 해주세요.

patients 배열을 어디에서 정렬합니까? 실제 patients 배열이 정렬되고 있습니까 아니면 tableView 대리자 메서드에서 정렬하고 실제로 정렬하지 않는 경우 patients?

삭제 된 개체의 인덱스가 실제 patients 배열에있는 인덱스와 동일하지 않기 때문입니다. 하나는 정렬되었지만 하나는 그렇지 않습니다. 이 때문에 잘못된 개체를 먼저 삭제 한 다음 tableView에서 삭제 될 것으로 예상하여 (삭제 될 셀을 움직일 수 있도록) 잘못된 개체가 삭제 되었기 때문에 충돌합니다.

+0

정렬 코드를 원합니까? – Hive7

+0

tableView 대리자 메서드를 게시 할 수 있습니까? 그리고 당신이 그것을 정렬하는 코드? – Jsdodgers

+0

어떻게 작동하도록 만들까요? – Hive7