【问题标题】:TableView ScrollToIndex When TextArea Is Focused当TextArea被聚焦时TableView ScrollToIndex
【发布时间】:2013-10-06 22:11:47
【问题描述】:

我有一个表格视图,其中一行包含一个按钮。当用户按下按钮时,我将一行添加到包含文本区域的表中。我希望新行位于 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});
}

当它运行时,就好像方法的最后一行没有被调用。新行会滚动到视图中,但不会一直滚动到顶部。它只会滚动到足以看到您在文本区域中输入的内容的视图中。然后您可以手动滚动该行,使其与顶部对齐,但我想自动执行此操作。有没有办法做到这一点?

提前致谢。

【问题讨论】:

  • 那个索引正确吗?

标签: ios uitableview titanium


【解决方案1】:

将焦点设置在 TextField 或 TextArea 上会触发滚动到您正在输入的位置。在 TextArea 中设置焦点后尝试调用 scrollToIndex。

【讨论】:

  • 这就是最后两行的作用。 newCommentAddedToView 事件只是调用文本字段的焦点。
  • 是的,但是触发事件是异步的,因此在您的情况下,tableView.scrollToIndex() 可能在 eventListener 处理 newCommentAddedToView 之前执行。您可以放置​​两个不同的 console.logs() 来确定代码执行的顺序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多