简体   繁体   English

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

[英]Reload section method in swift 3?

I have code written in objective cI want to convert this code into the swift3 code.我有用客观 c 编写的代码我想将此代码转换为 swift3 代码。

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

After converting using online tool it gave me below code but it does not work使用在线工具转换后,它给了我下面的代码,但它不起作用

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

Please tell me how to do it ?请告诉我怎么做?

Reload Section in Swift 3.0Swift 3.0 中重新加载部分

ie Reloading 0th section to 0th Section, Because tableview has default one section that index is 0.即重新加载第 0 节到第 0 节,因为 tableview 默认有一个索引为 0 的节。

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

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

For Swift 4对于 Swift 4

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

For Swift 5对于 Swift 5

In Swift 5 you can use this short & simple line.在 Swift 5 中,您可以使用这个简短的行。

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

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

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

Note that after the conversion NSIndexSet became IndexSet .请注意,转换后NSIndexSet变成了IndexSet IndexSet is the Swift overlay to the Foundation framework for NSIndexSet :索引集是斯威夫特覆盖为基础框架的NSIndexSet

The Swift overlay to the Foundation framework provides the IndexSet structure, which bridges to the NSIndexSet class and its mutable subclass, NSMutableIndexSet. Swift 覆盖到 Foundation 框架提供了 IndexSet 结构,它连接到 NSIndexSet 类及其可变子类 NSMutableIndexSet。 The IndexSet value type offers the same functionality as the NSIndexSet reference type, and the two can be used interchangeably in Swift code that interacts with Objective-C APIs. IndexSet 值类型提供与 NSIndexSet 引用类型相同的功能,并且两者可以在与 Objective-C API 交互的 Swift 代码中互换使用。 This behavior is similar to how Swift bridges standard string, numeric, and collection types to their corresponding Foundation classes.这种行为类似于 Swift 将标准字符串、数字和集合类型桥接到它们相应的 Foundation 类的方式。

If you checked the description of reloadSections method signature, you will note that it is:如果你查看了reloadSections方法签名的描述,你会注意到它是:

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

sections parameter is IndexSet but NOT NSIndexSet anymore. sections参数是IndexSet但不再是NSIndexSet了。

So, what you could do is:所以,你可以做的是:

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

OR I prefer to add IndexSet without even using the as IndexSet :或者我更喜欢添加 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