Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload only one section header in UITableView

I'm using a custom cell as a section header in my UITableView. In that cell there are three buttons. If any of the buttons are clicked in that section's cell, it should reload that custom section cell only, not any rows. Is this possible?

I was using the following code to reload that cell:

tableViewHome.reloadSections([1], with: UITableViewRowAnimation.none)

It's hiding my section cell and distorting my entire table.

UPDATE

I'm using UITableView and following code I'm using:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let cellHeader = tableViewHome.dequeueReusableCell(withIdentifier: "header") as! HeaderTableViewCell

        cellHeader.filter1btn.addTarget(self, action: #selector(filterBtnAction(_:)), for: .touchUpInside)
        cellHeader.filter2Btn.addTarget(self, action: #selector(filterBtnAction(_:)), for: .touchUpInside)
        cellHeader.filter3btn.addTarget(self, action: #selector(filterBtnAction(_:)), for: .touchUpInside)


        return cellHeader
    }


    @IBAction func filterBtnAction(_ sender: UIButton) {

        print(sender.tag)

        tableViewHome.reloadSections([1], with: UITableViewRowAnimation.none)



    }
like image 791
Abhishek Mitra Avatar asked Jul 03 '17 14:07

Abhishek Mitra


2 Answers

I'm a little unclear as to what's going on here, but it sounds like there is a UITableView concepts worth explaining here:

UITableView has its own concept of a cell, implemented as UITableViewCell, and its own concept of a header/footer, implemented as UITableViewHeaderFooterView.

Depending on which of these two you meant, there are a few things you can do to get the intended effect:

The UITableViewCell Approach:

If you're using a UITableViewCell as the first row of a section to act like a "header," and you just want to reload that row to the exclusion of the rest of the section, you can call yourTableViewInstance.reloadRows(at:with:) (Apple Documentation) This method takes an array of IndexPaths, and an animation style. You can pass in the indexPath of the one you want to reload.

The UITableViewHeaderFooterView Approach:

If you're using a proper UITableViewHeaderFooterView then you need to make sure that you're providing the appropriate view when reloading the section. Zack Shapiro outlines the steps you need to take in this answer:

  1. Create a class that's a subclass of UITableViewHeaderFooterView.
  2. Register it with your UITableView instance.
  3. Then in viewForHeaderInSection, you do let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderView") as! YourHeaderViewSubclass

The last thing he points out is this:

The deceptive thing is the function calls for a return of UIView? when it really needs a dequeuedReusableHeaderFooterView or reloadData will cause it to disappear.

It depends on which of these two implementation paths you're taking, but this should be enough information to point you in the right direction.

Edit:

Based on the code you added, it looks like you're calling yourTableViewInstance.dequeueReusableCell(withIdentifier:for:) instead of yourTableViewInstance.dequeueReusableHeaderFooterView(withIdentifier:) inside of viewForHeaderInSection.

You need to have a subclass of UITableViewHeaderFooterView and then call it correctly. Create that new subclass, then change this:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let cellHeader = tableViewHome.dequeueReusableCell(withIdentifier: "header") as! HeaderTableViewCell

   // ...

to this:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let cellHeader = tableViewHome.dequeueReusableHeaderFooterView(withIdentifier: "header") as! HeaderTableView

   // ...

You need to follow two steps here:

  1. Create a new class, subclassing UITableViewHeaderFooterView instead of UITableViewCell.
  2. Then use the appropriate class as outlined above.
like image 87
Moshe Avatar answered Sep 20 '22 15:09

Moshe


Yes, It is.

Let's say that this is implementation of your method:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let customCell = .... as! YourCustomCell 
    customCell.someLabel.text = "Some Data"
    //... filling your curstom cell
    return customCell
}

You can change it in this way

func updateHeaderView(headerView:YourCustomCell, section: Int) {
    customCell.someLabel.text = "Some Data"
    //... filling your curstom cell  
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let customCell = .... as! YourCustomCell 
    self.updateHeaderView(customCell, section)
    return customCell
}

And call again self.updateHeaderView(customCell, section) whenever you want, e.g.

func buttonClicked() {
   let customCell = self.tableView.headerView(forSection: 0) as! YourCustomCell
   self.updateHeaderView(customCell, 0)
}
like image 35
arturdev Avatar answered Sep 19 '22 15:09

arturdev