GayashanK
GayashanK

Reputation: 1215

Swift - Reload UITableView Header

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.

Upvotes: 9

Views: 17286

Answers (4)

Kazim Ahmad
Kazim Ahmad

Reputation: 15

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()

Upvotes: -2

Ebin Joy
Ebin Joy

Reputation: 3199

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.

Upvotes: -1

user3608500
user3608500

Reputation: 835

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.

Upvotes: 3

AkBombe
AkBombe

Reputation: 658

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

tableView.reloadSections(IndexSet(0..<1), with: .automatic)

Upvotes: 4

Related Questions