我有用目标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)
有关更多信息,请参阅swift3,请参阅this
expandableTableView.reloadSections(IndexSet(integer: gestureRecognizer.view!.tag), with: .automatic)
请注意,转换后NSIndexSet
成为IndexSet
。 IndexSet是NSIndexSet基金会框架的Swift叠加:
Swift覆盖到Foundation框架提供了IndexSet结构,该结构桥接到NSIndexSet类及其可变子类NSMutableIndexSet。 IndexSet值类型提供与NSIndexSet引用类型相同的功能,并且这两者可以在与Objective-C API交互的Swift代码中互换使用。此行为类似于Swift如何将标准字符串,数字和集合类型桥接到其对应的Foundation类。
如果您检查了reloadSections
方法签名的描述,您会注意到它是:
reloadSections(_ sections:IndexSet,带动画:UITableViewRowAnimation
sections
参数是IndexSet
但不再是NSIndexSet
。
那么,你能做的是:
_expandableTableView.reloadSections(NSIndexSet(index: gestureRecognizer.view.tag) as IndexSet, with: .automatic)
或者我更喜欢在不使用as IndexSet
的情况下添加IndexSet:
_expandableTableView.reloadSections(IndexSet(integer: gestureRecognizer.view!.tag), with: .automatic)