繁体   English   中英

在swift 3中重新加载部分方法?

[英]Reload section method in swift 3?

我有用客观 c 编写的代码我想将此代码转换为 swift3 代码。

[_expandableTableView reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];

使用在线工具转换后,它给了我下面的代码,但它不起作用

expandableTableView.reloadSections(IndexSet(index: gestureRecognizer.view!.tag), with: .automatic)

请告诉我怎么做?

Swift 3.0 中重新加载部分

即重新加载第 0 节到第 0 节,因为 tableview 默认有一个索引为 0 的节。

//let myRange: ClosedRange = 0...0

self.tableViewPostComments.reloadSections(IndexSet(integersIn: 0...0), with: UITableViewRowAnimation.top)

对于 Swift 4

self.tableViewPostComments.reloadSections(IndexSet(integersIn: 0...0), with: UITableView.RowAnimation.top)

对于 Swift 5

在 Swift 5 中,您可以使用这个简短的行。

self.tableViewPostComments.reloadSections(IndexSet(integersIn: 0...0), with: .top)

喜欢在swift3 中获取更多信息,请参阅

expandableTableView.reloadSections(IndexSet(integer: gestureRecognizer.view!.tag), with: .automatic)

请注意,转换后NSIndexSet变成了IndexSet 索引集是斯威夫特覆盖为基础框架的NSIndexSet

Swift 覆盖到 Foundation 框架提供了 IndexSet 结构,它连接到 NSIndexSet 类及其可变子类 NSMutableIndexSet。 IndexSet 值类型提供与 NSIndexSet 引用类型相同的功能,并且两者可以在与 Objective-C API 交互的 Swift 代码中互换使用。 这种行为类似于 Swift 将标准字符串、数字和集合类型桥接到它们相应的 Foundation 类的方式。

如果你查看了reloadSections方法签名的描述,你会注意到它是:

reloadSections(_部分:IndexSet,带动画:UITableViewRowAnimation

sections参数是IndexSet但不再是NSIndexSet了。

所以,你可以做的是:

_expandableTableView.reloadSections(NSIndexSet(index: gestureRecognizer.view.tag) as IndexSet, with: .automatic)

或者我更喜欢添加 IndexSet 甚至不使用as IndexSet

_expandableTableView.reloadSections(IndexSet(integer: gestureRecognizer.view!.tag), with: .automatic)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM