2009-09-24 5 views
32

UITableView를 주소록 편집기의 테이블처럼 동작 시키길 원합니다. 즉, 사용자가 편집을 눌러야하고 "새 카테고리 추가"행이 각 섹션 하단에 나타나야합니다.UITableView에서 삽입 행 사용하기

아래 코드를 사용하고 있지만 연락처에있는대로 전환이 원활하지 않은 것이 문제입니다. 대신 새로운 행이 갑자기 나타납니다. 애니메이션을 어떻게 얻을 수 있습니까?

또한 '새 카테고리 추가'행의 클릭에 어떻게 응답합니까? 현재 구현에서 행을 클릭 할 수 없습니다.

사용자가 편집을 시작할 때 데이터를 다시로드해야합니까? 그렇지 않으면 삽입 행이 그려지지 않기 때문에이 작업을 수행하고 있습니다.

감사합니다. 행에 대한 클릭에 응답

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 
    [self.tableView setEditing:editing animated:animated]; 
    [tableView reloadData]; 
} 

- (NSInteger)tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)section { 
    // ... 
    if(self.tableView.editing) 
     return 1 + rowCount; 
} 

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // ..... 
    NSArray* items = ...; 
    if(indexPath.row >= [items count]) { 
     cell.textLabel.text = @"add new category"; 
    } 
    // ... 

    return cell; 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSArray* items = ...; 

    if(indexPath.row == [items count]) 
     return UITableViewCellEditingStyleInsert; 

    return UITableViewCellEditingStyleDelete; 
} 
+2

이 (아래, 대답과 함께) 매우 도움이되었다 :

// Assuming swipeMode is a BOOL property in the class extension. - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath { // Invoked only when swiping, not when pressing the Edit button. self.swipeMode = YES; } - (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath { self.swipeMode = NO; } 

귀하의 코드를 변경해야 할 것입니다. 'tableView : editingStyleForRowAtIndexPath :'에서'=='을 사용하는 동안'tableView : cellForRowAtIndexPath :'의 행 개수 비교는'> ='을 사용합니다. 큰 문제는 아니지만 그것들간에 일관성이 있어야합니다. '> ='는 Insert 행의 우발적 인 이중 추가를 다루지 만,'=='는 그 상황을 초래할 수있는 코드 오류에 대한 예외를 던짐으로써 도움을 줄 것입니다. –

답변

36

한 가지가 빠졌습니다. 대신 reloadData를 호출 setEditing에서 :, 내가 했어야 :

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 
    [self.tableView setEditing:editing animated:animated]; // not needed if super is a UITableViewController 

    NSMutableArray* paths = [[NSMutableArray alloc] init]; 

    // fill paths of insertion rows here 

    if(editing) 
     [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom]; 
    else 
     [self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom]; 

    [paths release]; 
} 
+3

감사합니다 빌, 이것은 나에게 매우 유용했습니다. 나는 비슷한 코드에서 몇 시간을 망쳐 봤지만 제대로 이해하지 못했습니다. 이것은 극적으로 도움이되었습니다. – hkatz

+0

다행히 도왔습니다! – Bill

+0

commitEditingStyle : forRowAtIndexPath는 어떻습니까? Apple 문서에서는 테이블 뷰가 처음 편집 모드로 전환 될 때 setEditing이 호출된다고 말합니다 ... 그 시점에서 삽입/삭제할 행을 어떻게 알 수 있습니까? – shim

5

indexPath.row == [items count]를 들어, didSelectRowAtIndexPath 방법으로 수행 할 수 있습니다. 애니메이션의 경우 here, insertRowsAtIndexPaths:withRowAnimation: 메서드를 사용하는 것이 좋습니다. 그것을 사용하는 방법에 대한 게시물이 있습니다 here.

0

강조 표시된 솔루션의 원치 않는 부작용은 사용자가 하나 개의 단일 행을 스 와이프하면 행이 또한 삽입 "추가"이다 (강타 것을 제공 사용 가능). 다음 코드는이 딜레마를 해결 :

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 
    [self.tableView setEditing:editing animated:animated]; // not needed if super is a UITableViewController 

    if (!self.swipeMode) { 

     NSMutableArray* paths = [[NSMutableArray alloc] init]; 

     // fill paths of insertion rows here 

     if(editing) 
      [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom]; 
     else 
      [self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom]; 
     [paths release]; 
    } 
}