Swift 3 scrollToRowAtIndexPath成为scrollToRow

Tom*_*Tom 7 swift3

我正在转换一个Swift 2.3项目o Swift 3并挖掘数千个变化

我目前有这样的代码:

outletCatalog.scrollToRowAtIndexPath(myIndex, atScrollPoisition: .top, animated: false)
Run Code Online (Sandbox Code Playgroud)

但是,它显然被替换为

func scrollToRow(at: IndexPath, at: UITableViewScrollPosition, animated: Bool)
Run Code Online (Sandbox Code Playgroud)

但是 - 我不明白"at"的双参数命名,我的Google搜索没有产生任何结果.代码翻译工具只是表明了它

scrollToRow(at:at:animated)
Run Code Online (Sandbox Code Playgroud)

Ana*_*mje 13

在从Swift 2.3Swift 3.2版本的代码转换之后,您需要GCD main dispatch block使用自动滚动行到索引的新更改调用下面的代码.

DispatchQueue.main.async {
   let index = IndexPath(row: 10, section: 0) // use your index number or Indexpath
   self.tableCart.scrollToRow(at: index,at: .middle, animated: true) //here .middle is the scroll position can change it as per your need
}     
Run Code Online (Sandbox Code Playgroud)

我希望它对你有用.


支持 - Swift 3.1,3.2,4.1


hva*_*sam 2

根据 API 参考文档,scrollToRow(at:at:animated:) 滚动表视图,直到由索引路径标识的行位于屏幕上的特定位置。第一个参数的类型为 IndexPath(它指定要滚动到的部分和行)。第二个参数是 UITableViewScrollPosition 类型的枚举值,指定行在滚动末尾的放置位置(屏幕的顶部/中间/底部)。第三个参数只是激发了这个过程。

在您的代码中,您有:

outletCatalog.scrollToRowAtIndexPath(myIndex, atScrollPosition: .top, animated: false)
Run Code Online (Sandbox Code Playgroud)

可以替换为:

outletCatalog.scrollToRow(at: myIndex, at: .top, animated: false)
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查看此处: https://developer.apple.com/reference/uikit/uitableview/1614997-scrolltorow https://developer.apple.com/reference/uikit/uitableviewscrollposition