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

问题描述 投票:21回答:3

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

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

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

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

请告诉我怎么做?

ios arrays swift uitableview reloaddata
3个回答
34
投票

在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)

33
投票

有关更多信息,请参阅swift3,请参阅this

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

6
投票

请注意,转换后NSIndexSet成为IndexSetIndexSetNSIndexSet基金会框架的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)
© www.soinside.com 2019 - 2024. All rights reserved.