2013-08-05 2 views
3

내 UITableView 인스턴스에서 셀을 인덱스 : 0으로 이동하고 데이터를 다시로드하려고합니다. 문제는 셀이 ("reloadData"로 인해) 맨 위에 "스냅"된다는 것입니다. 내가 셀을 위로 움직일 수 있는지 궁금해서 몇 초 후 reloadData를 적용하십시오. Heres는 내 코드 : 메소드가 호출 된 후reloadData를 사용하여 애니메이션 사용

[_toDoItems removeObject:todoItem]; 

[_toDoItems insertObject:todoItem atIndex:0 ]; 





[self.tableView beginUpdates]; 

[self.tableView moveRowAtIndexPath:origIndexPath toIndexPath:0]; 

[self.tableView endUpdates]; 

sleep(1); 
[self.tableView reloadData]; 

이의 문제는, 모든 1 초 동안 일시 정지되고, 다음 애니메이션이 발생합니다 "스냅".

+1

뭐죠 수면 '의 사용 (1) ;'? 그것을 제거하십시오. – Souljacker

+3

'sleep()'을 사용해서는 안됩니다. 모든 비주얼을 처리하는 주 스레드를 포함한 모든 스레드를 차단합니다. 이것이 잠깐 기다린 다음 찰칵 때 보이는 이유입니다. 스냅은 잠금 장치가 해제되기를 기다리는 중입니다. 또한'reloadData'에는 애니메이션이 없기 때문에 "스냅하는"효과를줍니다. – Firo

+0

내 논리는 셀이 맨 위로 이동한다는 것입니다. 잠자기가 reloadData를 1 초 지연시킵니다. – Brian

답변

1

먼저 tableView에 beginUpdates의 업데이트가 필요하다고 말하십시오. 그런 다음 관련 섹션을 업데이트하십시오 (tableView에 섹션이 하나만있는 경우 섹션 번호에 0을 전달하십시오). 여기에 애니메이션을 지정하는 곳이 있습니다 - 당신이 원하는 효과를 내기 위해 주위를 놀 수 있습니다. 그 후에는 endUpdates을 tableView에 호출합니다. UITableViewRowAnimation에 대한 형식 정의가 지정

typedef enum { 
    UITableViewRowAnimationFade, 
    UITableViewRowAnimationRight, 
    UITableViewRowAnimationLeft, 
    UITableViewRowAnimationTop, 
    UITableViewRowAnimationBottom, 
    UITableViewRowAnimationNone, 
    UITableViewRowAnimationMiddle, 
    UITableViewRowAnimationAutomatic = 100 
} UITableViewRowAnimation; 

재생 주위에 당신이 원하는 어느 볼 수 있습니다. 심지어 UITableViewRowAnimationNone을 선택해도 좋은 효과를 낼 수 있습니다. 당신은 당신이 아래 - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation

표 업데이트 코드를 사용할 수 있습니다 얼마나 많은 부분에 따라 :

[self.tableView beginUpdates]; 
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone]; 
[self.tableView endUpdates]; 
+0

@brain 유용한 답변을 찾으면 ** upvote **하고 대답이 맞다면 ** 받아주세요 ** 대답하지 않은 질문 목록에서 질문을 삭제하는 데 도움이됩니다. * * – icodebuster

0

이 시도 :

[self.tableView beginUpdates]; 
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:origIndexPath 
                 inSection:0]] 
       withRowAnimation:UITableViewRowAnimationAutomatic]; 
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 
                 inSection:0]] 
       withRowAnimation:UITableViewRowAnimationAutomatic]; 
[self.tableView endUpdates]; 
+2

그리고 위에서 언급 한 다른 사람들처럼 sleep()을 사용하지 마십시오. 나는 그것을 금지한다! :) –

+0

심지어 조정 withRowAnimation : 뭔가 다른, 정말 문제를 해결하지 않습니다. 이상한 스냅/깜박임을 방지하지만 애니메이션이 없으면 셀이 순간적으로 즉시 나타납니다. :/ – Brian

1

를 해결.

 [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(briefPause) userInfo:nil repeats:NO]; 

제가

[self.tableView beginUpdates] 
.... 

과 동일한 방법으로 상기 사용하지만 그냥 새로운 기능 briefPause했다 :

[self.tableView reloadData]; 
+0

실제로 작동하지만 적은 시간을 사용하는 것이 좋습니다. [NSTimer scheduledTimerWithTimeInterval : 0.2 target : self selector : @selector (briefPause) userInfo : nil repeats : NO]; 여전히 작동하지만 더 부드럽게 보입니다. – Renexandro

관련 문제