Moje rozwiązanie w Swift:
class MyTableViewController: UITableViewController {
var sectionHeaderView:UIView?
...
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
sectionHeaderView = UIView(frame: CGRectMake(0, 0, self.tableView.frame.size.width, 30))
sectionHeaderView?.backgroundColor = UIColor.grayColor()
var button = UIButton(frame: CGRectMake(0, 0, self.tableView.frame.size.width, 30))
button.backgroundColor = UIColor.darkGrayColor()
button.setTitle("collapse/expand", forState: .Normal)
button.addTarget(self, action: "collapseOrExpandSectionHeader", forControlEvents: .TouchUpInside)
sectionHeaderView?.addSubview(button)
return sectionHeaderView
}
...
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if let sectionHeader = sectionHeaderView {
return view.frame.size.height
} else {
return 30.0
}
}
...
func collapseOrExpandSectionHeader() {
if let sectionHeader = sectionHeaderView {
let headerHeight:CGFloat
if sectionHeader.frame.size.height == 200 {
headerHeight = 30.0
} else {
headerHeight = 200.0
}
UIView.animateWithDuration(0.3, animations: {
self.tableView?.beginUpdates()
sectionHeader.frame.size.height = headerHeight
self.tableView?.endUpdates()
})
}
}
dzięki @ prendio2 że działa dobrze. nie udało się jednak usunąć widoku (co właśnie próbowałem zrobić). ale jak tylko przewinąłem to zostało usunięte, więc to jest ok :) – jasongregori
dla każdego, kto ma ten sam przypadek użycia co ja (chcą usunąć widok), możesz zachować odnośnik do niego i użyć metody "removeFromSuperview" na to podczas animacji, aby ukryć to poprawnie. – jasongregori
To działa dobrze, ale obszar, który pojawia się, obejmuje moją pierwszą UITableViewCell. Jakieś pomysły? Jakikolwiek sposób, aby przesunąć tableView w dół, kiedy to się dzieje? – arooo