7

I'm try to reload only UITableView section header without reloading whole section or reload whole table. Thanks in advance.

note :- I follow answers of already answered question. But I still cannot figure out this.

GayashanK
  • 1,195
  • 2
  • 13
  • 27

4 Answers4

3

Use this method to reload section. Add index set of section which you want to reload.

tableView.reloadSections(IndexSet(0..<1), with: .automatic)
AkBombe
  • 638
  • 4
  • 10
3

There is no standard method to do that, you can use tableView:viewForHeaderInSection: function to get the section header from UITableView and do the updates.

user3608500
  • 835
  • 4
  • 10
-1

Add-on to user3608500 answer

Reload Header view only

I think your header view class is extending UITableViewHeaderFooterView class. Add a function in the extension

extension UITableViewHeaderFooterView{
     func reloadHeaderCell(){
        preconditionFailure("This method must be overridden")
    }
}

Now override this in your Header class as below

class HeaderView: UITableViewHeaderFooterView {
   override func reloadHeaderCell() {
      ////// add your logic to reload view
    }
}

Now you can simply call below line to refresh views

self.tableView?.headerView(forSection:0)?.reloadHeaderCell()

Reload entire Section view

For reloading entire section/sections view including rows(here section 0 and 2), you can call like below

  self.tableView?.reloadSections([0,2], with: .none)

Reload entire table view

For reloading entire table view, you can call like below

self.tableView?.reloadData()

Reload particular row

For reloading particular row/rows, you can call like below

self.tableView?.reloadRows(at: [IndexPath(item: 1, section: 2), IndexPath(item: 4, section: 3)], with: .none)

Above code will reload 1st item in 2nd section and 4th item in 3rd section.

Ebin Joy
  • 2,690
  • 5
  • 26
  • 39
-2

How i handled in for my app was

override func viewDidLayoutSubviews() {
   tableview.tableHeaderView = customHeader
} 

then to reload data simply call the method:

self.view.layoutSubviews()