Uitableview:Viewforheaderinsection: Not Called During Reloaddata:

viewForHeaderInSection: not called when reloadData: is called

You should implement heightForHeaderInSection: and set the height for the header to a value > 0.

tableView(_:viewforHeaderInSection:) Not Being Called

I found the problem.

It turns out there was an auxiliary object overtaking the role of table view delegate and hiding it.

This class:

  • Adopts the UITableViewDelegate protocol and implements most of its methods (but not the header/footer-related ones),
  • Sets itself as the table view's delegate inside the table view controller's viewDidAppear() method,
  • Keeps a reference to the table view controller,
  • When it's implementation of (say) tableView(_:didSelectRowAtIndexPath:) is called, it in turn calls the table view controller's equivalent method, giving the impression that the view controller is still the delegate:

    class ManInTheMiddle: NSObject, UITableViewDelegate {
    var formerDelegate: UITableViewController?

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    formerDelegate?.tableView(tableView, didSelectRowAt: indexPath)
    // Do some other stuff...
    }
    }

This is why some methods of UITableViewDelegate where being called on my table view controller and some weren't.

I discovered this by overriding the table view's delegate property:

class DebugTableView: UITableView {

// ...
override var delegate: UITableViewDelegate? {
get {
return super.delegate
}
set (newValue) {
super.delegate = newValue // < BREAKPOINT HERE
}
}

...out of desperation (there was no strong reason to suspect the delegate was swapped, being that most methods worked...)

<rant>Damn, do I hate these kind of dirty hacks... This is almost #define true false territory, sheesh...
</rant>

TableView viewForHeaderInSection Won't Resume First Responder

Okay, these approaches are useful but I discovered here is the only answer that works:

First, resign the first responder after the textField disappears before calling reloadData()

 private var searchMode = false {
didSet {
if let tv = self.tableView.headerView(forSection: 0), let stv = tv as? SearchTableViewHeader {
stv.searchField.resignFirstResponder()
}
tableView.reloadData()
}
}

Then, set first responder at the very last tableView function. This is because there is unexpected behavior is setting first responder in viewForHeader

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if searchMode, let stv = view as? SearchTableViewHeader {
stv.searchField.becomeFirstResponder()
}
}

Reload sections is not calling viewForHeaderInSection

I did this in my project . what i did is created a prototype cell inside a tableview and add label and a button in the storyboard.

Created Tableviewcell class

class mainTableHeaderViewCell: UITableViewCell {

@IBOutlet weak var _btnShowHide: UIButton!
@IBOutlet weak var _lblSectionHeaderName: UILabel!
}

in controller

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

let header = tableView.dequeueReusableCellWithIdentifier("mainTableHeaderViewCell")! as! mainTableHeaderViewCell
header._lblSectionHeaderName.text = objTempDeficiencyList[section][0].sectionName
// sectionNameList[section].DESCRIPTION
header._btnShowHide.addTarget(self, action: "hideSection:", forControlEvents: .TouchUpInside)
if hiddenSections.contains(section) {
header._btnShowHide.setImage(UIImage(named: "expand"), forState: UIControlState.Normal)
}
else {
header._btnShowHide.setImage(UIImage(named: "uparrow"), forState: UIControlState.Normal)
}

header._btnShowHide.tag = section
return header.contentView
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if hiddenSections.contains(section) {
return 0
}
return List[section].count // getRowCount(section)
}

Hope this help .



Related Topics



Leave a reply



Submit