2013-10-06 2 views
1

저는 tableview가 있고 행 중 하나에는 버튼이 있습니다. 사용자가 버튼을 누르면 텍스트 영역이 포함 된 테이블에 행을 추가합니다. 새로운 행을 tableview 맨 위에 놓고 키보드를 열었 으면합니다. 내가 시도한 것은 다음과 같습니다.TextArea에 포커스가있는 경우 TableView ScrollToIndex

function addNewComment() { 
    var newComment = new NewComment(tableView.data[0].rows.length); 

    //I add the spacer so that if the added row is the last in the table, it can still be at the top of the view 
    var spacer = Ti.UI.createTableViewRow({ 
     height:250, 
     backgroundColor: 'white' 
    }); 

    //There are 6 rows of information in the table before the comments start 
    tableView.appendRow(spacer, {animated:false}); 
    tableView.insertRowAfter(5, newComment, {animationStyle:Titanium.UI.iPhone.RowAnimationStyle.DOWN}); 

    //The listener for this just focuses the text area 
    Ti.App.fireEvent('newCommentAddedToView'); 
    tableView.scrollToIndex(6,{animated:true,position:Ti.UI.iPhone.TableViewScrollPosition.TOP}); 
} 

이 메서드를 실행하면 메서드의 마지막 줄이 호출되지 않은 것처럼 보입니다. 새 행은보기로 스크롤되지만 맨 위로는 이동하지 않습니다. 텍스트 영역에서 입력 내용을 볼 수있을 정도로만 화면을 스크롤합니다. 그런 다음 상단과 정렬되도록 행을 수동으로 스크롤 할 수 있지만이 작업은 자동으로 수행하고 싶습니다. 이것을 달성 할 방법이 있습니까?

미리 감사드립니다.

+0

해당 색인이 맞습니까? – Anand

답변

0

TextField 또는 TextArea에 포커스를 설정하면 입력중인 위치로 스크롤됩니다. TextArea에 이미 포커스가 설정된 후에 scrollToIndex를 호출 해보십시오.

+0

그것이 마지막 두 줄의 기능입니다. 'newCommentAddedToView' 이벤트는 텍스트 필드의 포커스만을 호출합니다. – eliot

+0

예, 발생 이벤트는 비동기식이므로 eventViewener가 newCommentAddedToView를 처리하기 전에 tableView.scrollToIndex()가 실행됩니다. 두 개의 다른 console.logs()를 사용하여 코드가 실행되는 순서를 확인할 수 있습니다. – daniula

관련 문제