Reload section method in swift 3?

58,719

Solution 1

Reload Section in Swift 3.0

i.e Reloading 0th section to 0th Section, Because tableview has default one section that index is 0.

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

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

For Swift 4

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

For Swift 5

In Swift 5 you can use this short & simple line.

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

Solution 2

do like in swift3 for more information see this

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

Solution 3

Note that after the conversion NSIndexSet became IndexSet. IndexSet is the Swift overlay to the Foundation framework for NSIndexSet:

The Swift overlay to the Foundation framework provides the IndexSet structure, which bridges to the NSIndexSet class and its mutable subclass, 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. This behavior is similar to how Swift bridges standard string, numeric, and collection types to their corresponding Foundation classes.

If you checked the description of reloadSections method signature, you will note that it is:

reloadSections(_ sections: IndexSet, with animation: UITableViewRowAnimation

sections parameter is IndexSet but NOT NSIndexSet anymore.

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:

_expandableTableView.reloadSections(IndexSet(integer: gestureRecognizer.view!.tag), with: .automatic)
Share:
58,719

Related videos on Youtube

TechChain
Author by

TechChain

I have 4+ of experience of overall experience.Currently working on Hyperledger Fabric blockchain framework. I have strong knowledge of Objective c, Swift.I have developed lot of apps from chat app, finance apps,location & map based apps.

Updated on September 13, 2020

Comments

  • TechChain
    TechChain almost 4 years

    I have code written in objective c.I want to convert this code into the swift3 code.

    [_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 ?

    • Ahmad F
      Ahmad F over 7 years
      "does not work" means... ? what does the error says?
  • mfaani
    mfaani over 6 years
    confused. Does this mean you can't just do: _expandableTableView.reloadSections([0]), with: .automatic) <-- This should reload only section 0
  • Mandeep Singh
    Mandeep Singh over 5 years
    Thanks Sir, it helps me.