2012-08-31 1 views
0

세 개의 섹션이있는 UITableView가 있으며 두 번째 섹션에는 편집 모드에서 삽입 및 삭제 표시기를 보여주는 테이블이 있습니다. cellForRowAtIndexPath에 삽입 행의 셀을 추가합니다. 편집 할 때 YES입니다. 또한 테이블이 편집 모드로 들어가면 섹션 수를 줄여서 세 번째 섹션이 표시되지 않습니다 (편집 모드에서 숨길 버튼이 있음). setEditing에서 [self.tableView reloadData]를 호출하지 않으면 삽입 행이 표시되지 않지만 호출 할 때 애니메이션이 없습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 편집 모드의 UITableView는 애니메이션을 죽이는 reloadData가 호출되지 않는 한 삽입을 표시하지 않습니다.

- (void)setEditing:(BOOL)flag animated:(BOOL)animated 

{ 
    [super setEditing:flag animated:YES]; 
    [self.tableView setEditing:flag animated:YES]; 
    //unless i add [self.tableView reloadData] i don't see the + row, but then there is no animation 
    [self.tableView reloadData]; 

내가 cellForRowAtIndexPath

if (indexPath.row == [[[self recipe] tasks] count]) 
{ 
    cell.textLabel.text = @"Add task..."; 
    cell.detailTextLabel.text = @""; 

크게 감사합니다 어떤 도움에 내가이 일을하고 삽입 행을 추가하려면이

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return self.editing ? 2 : 3; 
} 

를하고있는 중이 야 섹션의 수를 결정합니다. 나는 이것에 얼마나 많은 시간을 낭비했는지 말할 때 당혹 스럽다!

+0

추가 정보 : 내가 reloadData를하지 않으면 (1) 나는 삽입 행을 볼 수 없습니다 또한 제 3 섹션은 사라지지 않습니다. 위와 같이 모든 것이 reloadData와 잘 맞지만 애니메이션이 없습니다. – ToddB

답변

2

UITableView 님의 업데이트 방법을 사용해야합니다. 자세한 내용은 Apple의 comprehensive guide을 확인하십시오. 그러나이 코드 스 니펫을 통해 아이디어를 얻을 수 있습니다. 테이블보기가 편집 모드를 벗어날 때 반대의 작업을 수행해야합니다.

NSIndexPath *pathToAdd = [NSIndexPath indexPathForRow:self.recipe.tasks.count section:SECTION_NEEDING_ONE_MORE_ROW]; 
NSIndexSet *sectionsToDelete = [NSIndexSet indexSetWithIndex:SECTION_TO_DELETE]; 
[self.tableView beginUpdates]; 
[self.tableView insertRowsAtIndexPaths:@[ pathToAdd ] withRowAnimation:UITableViewRowAnimationAutomatic]; 
[self.tableView deleteSections:sectionsToDelete withRowAnimation:UITableViewRowAnimationAutomatic]; 
[self.tableView endUpdates]; 
0

감사합니다. Carl. 완전한. 나는 Apple 문서를 여러 번 읽었지만 이해하지 못했습니다. Google 예제가 나를 잘못된 경로로 보냈습니다. 문제는 해결되었고 정말 멋져 보입니다. :)

NSIndexPath *pathToAdd = [NSIndexPath indexPathForRow:self.recipe.tasks.count section:1]; 
NSIndexSet *sectionsToDelete = [NSIndexSet indexSetWithIndex:2]; 
[self.tableView beginUpdates]; 
[self.tableView insertRowsAtIndexPaths:@[ pathToAdd ] withRowAnimation:UITableViewRowAnimationAutomatic]; 
// update the datasource to reflect insertion and number of sections 
// I added a 'row' to my datasource for "Add task..." 
// which is removed during setEditing:NO 
[self.tableView deleteSections:sectionsToDelete withRowAnimation:UITableViewRowAnimationAutomatic]; 
[self.tableView endUpdates]; 

토드