【问题标题】:Reload section method in swift 3?swift 3中的重新加载部分方法?
【发布时间】:2017-03-15 13:48:41
【问题描述】:

我有用objective 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


【解决方案1】:

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)

【讨论】:

  • 谢谢先生,对我有帮助。
【解决方案2】:

请注意,转换后NSIndexSet 变为IndexSetIndexSetNSIndexSet 的 Foundation 框架的 Swift 覆盖:

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

如果你检查了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)

【讨论】:

  • 困惑。这是否意味着你不能只做:_expandableTableView.reloadSections([0]), with: .automatic) 0
【解决方案3】:

swift3 中点个赞以获取更多信息,请参阅this

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多