Created
May 2, 2017 12:52
-
-
Save iRiziya/cd8d73fdd2a9e5105cd18863f41f3784 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { | |
@IBOutlet var tableView: UITableView! | |
var headerView = UIView() | |
var selectedSections = NSMutableArray() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// self.tableView.reloadData() | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func numberOfSections(in tableView: UITableView) -> Int { | |
return 3 | |
} | |
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { | |
return 60 | |
} | |
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { | |
headerView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.frame.size.width, height: 50)) | |
headerView.backgroundColor = UIColor.orange | |
headerView.tag = section | |
headerView.isUserInteractionEnabled = true | |
headerView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: (!self.selectedSections.contains(section)) ? #selector(self.expand(sender:)) : #selector(self.collapse(sender:)))) | |
return headerView | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return self.selectedSections.contains(section) ? 2 : 0 | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")! | |
cell.textLabel?.text = "Row \(indexPath.row)" | |
return cell | |
} | |
func expand(sender:UIGestureRecognizer){ | |
self.selectedSections.add(sender.view!.tag) | |
self.tableView.reloadSections([sender.view!.tag], with: .automatic) | |
} | |
func collapse(sender:UIGestureRecognizer){ | |
self.selectedSections.remove(sender.view!.tag) | |
print(self.selectedSections) | |
self.tableView.reloadSections([sender.view!.tag], with: .automatic) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment