在Swift中记住UITableView上的最后一个scrollview位置

时间:2016-02-21 04:07:30

标签: swift

我有一个很大程度上填充的TableView在我的应用程序中运行50多个单元格行。当我向下滚动到特定单元格并深入到详细视图控制器并再次返回时,表格一直滚动到顶部即可。我已经看过以下类似的问题了

How can I get the UITableView scroll position so I can save it?

Setting scroll position in UITableView

Remembering scroll position on UITableView

https://grokswift.com/programmatic-uitableview-scrolling/

我尝试从上面的链接和答案中解决问题但是,到目前为止我仍然没有运气。我正在发布我的尝试,如下所示。

方法1:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

   self.myTableView.reloadData()
   self.myTableView.setContentOffset(CGPointMake(0, -20), animated: false)
}

方法2:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

   self.myTableView.reloadData()
           dispatch_async(dispatch_get_main_queue(), {() -> Void in
        let rowIndexPath: NSIndexPath = NSIndexPath(forRow: 3, inSection: 0)
        self.myTableView.scrollToRowAtIndexPath(rowIndexPath, atScrollPosition: .Middle, animated: false)

    })
}

方法3:

func scrollToSelectedRow() {
  let selectedRows = self.tableView.indexPathsForSelectedRows
  if let selectedRow:NSIndexPath = selectedRows?[0] {
    self.tableView.scrollToRowAtIndexPath(selectedRow, atScrollPosition: .Middle, animated: true)
  }
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

   self.myTableView.reloadData()
   func scrollToSelectedRow()
}

提前感谢...

0 个答案:

没有答案
相关问题