2010-08-22 4 views
0

ive는 삭제를 위해 sdk 표준 코드를 사용했지만 삭제 버튼을 누르면 충돌이 발생합니다. 내가 NSMutableArray를 대신 tableFavoritesData 모두 노력이 코드테이블보기 셀 삭제 now

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

if (editingStyle == UITableViewCellEditingStyleDelete) { 
// Delete the row from the data source 
[tableView deleteRowsAtIndexPaths:[tableFavoritesData arrayWithObject:indexPath] withRowAnimation:YES]; 
} 

} 

을 사용하고 그러나 아무것도 작동하지 않습니다. 누군가 나를 도울 수 있습니까?

답변

2

음, 기본적으로 싶은 것은 :

  1. 데이터 소스 (배열)에서 행을 삭제합니다.
  2. 데이터 원본에서 행을 삭제했음을 테이블보기에 알립니다.

올바른 코드는 아마과 같아야합니다

if (editingStyle == UITableViewCellEditingStyleDelete) { 
[tableFavoritesData removeObjectAtIndex:indexPath.row]; 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
} 

편집 : 나는 다른 오류를 발견하지 않았다.

YES 또는 NO를 전달하는 것이 아니라 애니메이션의 유형을 지정해야합니다. 예 : UITableViewRowAnimationFade. 가능한 UITableViewRowAnimation 값 here을 확인하십시오.

편집 2 : 아래의 주석 (코멘트 형식화) : 문서에서 NSNotificationCenter을 확인하십시오. 특히 addObserver : selector : name : object : 및 postNotificationName : object : methods를 확인하십시오. 다른 뷰 컨트롤러에서

(아마의 viewDidLoad 방법) :

if (editingStyle == UITableViewCellEditingStyleDelete) { 
... 
[[NSNotificationServer defaultCenter] postNotificationName:@"RowDeleted" object:self userInfo:[NSDictionary dictionaryWithObject:indexPath forKey:@"IndexPath"]]; 
    } 

그냥 당신이를 할당 해제 때 알림 센터에서 관찰자를 제거해야한다는 것을 기억

[[NSNotificationServer defaultCenter] addObserver:self selector:@selector(deletedRow:) name:@"RowDeleted" object:nil]; 

-(void) deletedRow:(NSNotification*) notification 
{ 
    NSDictionary* userInfo = [notification userInfo]; 
    NSIndexPath indexPath = [userInfo objectForKey:@"IndexPath"]; 
// your code here 
} 

및 행을 삭제하는 동안 다른 UIViewController :

[[NSNotificationServer defaultCenter] removeObserver: self]; 

희망 마 많은 오류가 발생했습니다. 저는 XCode atm에 액세스 할 수 없습니다.

+0

대단히 감사합니다. (내 질문에 sidenote 다른 컨트롤러에서 테이블이있는 하나의 복사 된 데이터 배열이 있습니다. 전체 배열을 다시 복사 할 필요없이 그 배열에서 개체를 제거 할 수있는 방법이 있다면 다시 nsuserdefault if 제거 되었습니까?) –

+0

답변에 샘플 코드를 추가 했으므로 이미 편집 된 내 의견을 편집 할 수 없습니다. 일반적으로 다른 ViewController를 옵저버로 등록하고 객체 삭제시 알림을 게시하려고합니다. – Mayjak

0

콘솔을 보면 모델 (데이터 구조)이 테이블의 예상과 일치하지 않는다고 말할 수 있습니다. 나는. 위임 방법

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

은 이전보다 작게 반환해야합니다.

또한 [tableFavoritesData arrayWithObject:indexPath]은 매우 이상하게 보입니다. 어쩌면 여기에 [NSArray arrayWithObject:indexPath]을 원할 것입니다. 그리고 먼저 모델에서 데이터를 제거하십시오.

관련 문제