簡體   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