0

I have custom header views made using UITableViewCell nib. Each of the custom header has buttons and pictures that need to be update based on different actions that user can take with buttons in the header.

I'm having difficult getting reference to the headerView to update its UI.

I obviously cannot use tableView.cellForRowAtIndexPath as it is a header not a tableview cell. I cannot use tableview.headerViewForSection since it was made with a UITableViewCell not a UITableViewHeaderFooterView.

I've also tried using

tableView.reloadSections(indexSet, withRowAnimation: UITableViewRowAnimation.None)

but this yields unwanted stuttering/glitchy UI reload animations even when set to UITableViewRowAnimation.None. The glitch appears to be the header disappearing and then reappearing from the top and the footer view slide down from the top also.

also have tried

tableView.beginUpdates()
tableView.endUpdates()

This has same glitchy effect.

and

let header = tableView.headerViewForSection(button.tag)
//codes to update headerview here
header?.setNeedsDisplay()
header?.setNeedsLayout()

This has no effects.

Any help would be greatly appreciated!

My codes look like this:

Register Nib:

let stickyHeaderNib = UINib(nibName: "Moment_StickyHeaderCell", bundle: nil)
    tableView.registerNib(stickyHeaderNib, forCellReuseIdentifier: "moment_stickyHeaderCell")

Then:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let momentHeader = tableView.dequeueReusableCellWithIdentifier("moment_stickyHeaderCell") as! Moment_StickyHeaderCell

//various buttons and objects here

momentHeader.followButton.addTarget(self, action: "followButtonPressed:", forControlEvents: UIControlEvents.TouchDragInside)

return momentHeader}

Then selector for button:

 func followButtonPressed (button: UIButton) {
    let moment = moments[button.tag]


    let point = tableView.convertPoint(CGPointZero, fromView: button)

    if let indexPath = tableView.indexPathForRowAtPoint(point)  {
}

//here's where I can't get reference to the headerView as mentioned above using .cellForRowAtIndexPath, .headerViewForSection

   }
Tuan Anh Vu
  • 760
  • 1
  • 7
  • 26

2 Answers2

1

I find this post answer [https://stackoverflow.com/a/3611661/4905076] witch is says that calling:

self.tableView.beginUpdates()
self.tableView.endUpdates()

// forces the tableView to ask its delegate/datasource the following:
//   numberOfSectionsInTableView:
//   tableView:titleForHeaderInSection:
//   tableView:titleForFooterInSection:
//   tableView:viewForHeaderInSection:
//   tableView:viewForFooterInSection:
//   tableView:heightForHeaderInSection:
//   tableView:heightForFooterInSection:
//   tableView:numberOfRowsInSection:

So you should update your section manually on followButtonPressed and call begin/end updates.

Community
  • 1
  • 1
Lucho
  • 1,024
  • 1
  • 15
  • 24
-1

button is subview of header view. You can get via button.superview

Check Image

kekkeme
  • 942
  • 1
  • 7
  • 10
  • Great idea, but I was unable to cast the button.superview as a the custom header class. if let headerView = button.superview as? Moment_StickyHeaderCell...button.superview is UITableviewContentview – Tuan Anh Vu Feb 14 '16 at 21:36
  • One more superview than. button.superview.superview will give you the stickyheadercell. Also you should be careful. You can make recursive method that goes superview until right stickyheadercell class – kekkeme Feb 15 '16 at 07:54