Ios 聚焦TextArea时的TableView滚动索引

Ios 聚焦TextArea时的TableView滚动索引,ios,uitableview,titanium,Ios,Uitableview,Titanium,我有一个tableview,其中一行包含一个按钮。当用户按下按钮时,我会在包含文本区域的表中追加一行。我希望新行位于tableview的顶部并打开键盘。以下是我尝试过的: 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 t

我有一个tableview,其中一行包含一个按钮。当用户按下按钮时,我会在包含文本区域的表中追加一行。我希望新行位于tableview的顶部并打开键盘。以下是我尝试过的:

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});
}
当它运行时,就好像没有调用方法的最后一行一样。新行将滚动到视图中,但不会一直滚动到顶部。它只会滚动到视图中,足以看到您在文本区域中键入的内容。然后,您可以手动滚动该行,使其与顶部对齐,但我希望自动执行此操作。有没有办法做到这一点


提前感谢。

将焦点设置在TextField或TextArea上会触发滚动到您正在键入的位置。在文本区域中设置了焦点后,尝试调用scrollToIndex。

该索引正确吗?最后两行就是这样做的。
newCommentAddedToView
事件只调用文本字段上的焦点。是的,但触发事件是异步的,因此在您的示例中,tableView.scrollToIndex()可能是在eventListener处理newCommentAddedToView之前执行的。您可以放置两个不同的console.logs(),以确保代码的执行顺序。