2010-04-21 3 views
7

iPhone SDK 3.1.3을 사용하고 있습니다. 다른 컨트롤러에서 데이터를 가져 오는 UITableViewController 있습니다. 테이블 뷰는 메인 뷰에 서브 뷰로 추가되지만 프레임은 보이지 않도록 설정됩니다. 테이블 뷰 프레임이 업데이트되고 단추를 두드리면 기본보기 위로 슬라이드됩니다.UITableView 특정 위치로 스크롤

테이블보기가 나타나고 마지막 행으로 스크롤됩니다. 마지막 행을 선택하면 더 많은 데이터가있는 표가 다시로드됩니다. 테이블이 더 많은 데이터로 업데이트됩니다. 스크롤 위치가 항상 맨 위를 제외하고는 모두 잘 작동합니다.

더 많은 데이터를로드하기 위해 클릭 한 마지막 행으로 스크롤 위치가 필요합니다. 스크롤 위치를 저장하고 더 많은 데이터를로드 한 후 다음 코드를 호출합니다. 문제없이 실행되지만 스크롤 위치는 항상 맨 위입니다.

[theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:savedScrollPosition animated:NO]; 

위의 내용은 효과가없는 것으로 보입니다. ViewWillAppear:ViewDidAppear:이 발생하지 않으며보기 컨트롤러가 코드로 인스턴스화 된 경우 이러한 이벤트는 발생하지 않는다고 전합니다. 내가 클릭 한 행에 있도록 테이블을 다시로드 한 후 ([theTableView reloadData]) 스크롤 위치를 설정하는 방법과시기를 알아내는 데 도움이됩니다. 즉 실제 코드이고 가정 theTableView가 nil이 아닌 경우

코드는 scrollToRowAtIndexPath이기 때문에 "에 ... 응답하지 않을 수 있습니다"라는 경고를 받고해야, 테이블보기를 & 스크롤

////performAction will notify the tableviewcontroller which will result in didPerformAction being called 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.row == lastRow) 
    { 
     savedScrollPosition = lastRow; 
     //perform the action 
     [controller performAction]; 
    } 
} 

- (void) didPerformAction:(NSNotification *)obj 
{ 
    [theTableView reloadData]; 
    [theTableView 
    scrollToRowAtIndexPath: [NSIndexPath indexPathForRow:savedScrollPosition inSection:0] 
    atScrollPosition:UITableViewScrollPositionBottom 
    animated:NO]; 
} 

답변

28

이 트릭을 할 것 같았다.

[theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO]; 
CGPoint point = theTableView.contentOffset; 
point .y -= theTableView.rowHeight; 
theTableView.contentOffset = point; 
+2

이 명령문의 savedScrollPosition은 무엇입니까? –

0

를 다시로드 철자가 잘못되었습니다.

둘째, atScrollPosition 매개 변수는 화면에서 목표 행을 배치 할 위치를 나타내는 UITableViewScrollPosition 열거 형 값을 필요로합니다.

이 시도 :

[theTableView scrollToRowAtIndexPath: 
       [NSIndexPath indexPathForRow:savedScrollPosition inSection:0] 
       atScrollPosition:UITableViewScrollPositionBottom 
       animated:NO]; 
+0

미안하지만 오타였습니다. 나는 그것을 편집했다. 글쎄, 나는 아래쪽으로 스크롤하고 싶지 않다. 클릭 한 행을 스크롤해야합니다.이 행은 중간 정도이지만 매번 중간이 아닙니다. 그럼에도 불구하고, 나는 이것을 시험해 보았고 이상하게도 여전히 정상에 머물러 있습니다. – Dave

+0

이것이 호출되는 메소드를 표시하십시오. savedScrollPosition의 값을 보여주는 호출 전에 NSLog를 넣으십시오. atScrollPosition은 행 번호를 참조하지 않고 화면의 현재 상대 위치를 나타냅니다. – DyingCactus

+0

오, 알았어. 나는 코드를 업데이트했다. – Dave

10

그것은 더 나은 모양 당신이 reloadData를 호출하는 대신 행을 삽입 할 수있는 경우 스크롤 위치가 고정 된 상태로 유지됩니다. 대신 스크롤있는 UIScrollView를 사용하여 스크롤 jQuery과 사용의

[theTableView beginUpdates]; 
[theTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:animation]; 
// make sure the dataSource will return new rows before calling endUpdates 
[theTableView endUpdates]; 

:

savedOffset = [theTableView contentOffset]; 

그런 다음 복원 :

[theTableView setContentOffset:savedOffset]; 
+0

오, 덕분에, 방금 contentaffset을 설정하여 답변을 게시했습니다. 삽입 행 팁 주셔서 감사합니다. – Dave

관련 문제