-
-
Save ThakurSahabUPse/7aeea984dd40bb20425db2ac002b868e to your computer and use it in GitHub Desktop.
Swift collaspable or expandable tableview
This file contains hidden or 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
| // | |
| // AccordionTableView.swift | |
| // PracticePro | |
| // | |
| // Created by Muzahidul Islam on 5/26/15. | |
| // | |
| import UIKit | |
| class AccordionTableView: UITableViewController { | |
| var groupArray = [String]() | |
| var boolArray : [String]! | |
| var dataDic = [String : [String]]() | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| self.title = "Accordion Table" | |
| groupArray = ["A","B"] | |
| boolArray = [String](count: groupArray.count, repeatedValue: "0") | |
| let groupA = ["BANGLADESH","INDIA","PAKISTHAN","SOUTH AFRICA"] | |
| let groupB = ["AUSTRALIA","ENGLAND","SRILANKA","WEST INDIES"] | |
| dataDic["A"] = groupA | |
| dataDic["B"] = groupB | |
| } | |
| override func didReceiveMemoryWarning() { | |
| super.didReceiveMemoryWarning() | |
| // Dispose of any resources that can be recreated. | |
| } | |
| override func numberOfSectionsInTableView(tableView: UITableView) -> Int{ | |
| return groupArray.count | |
| } | |
| override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ | |
| let boolForSec = boolArray[section] as String | |
| if (boolForSec.toInt() != 0) { | |
| var arr = dataDic[groupArray[section]] | |
| return arr!.count | |
| }else { | |
| return 0 | |
| } | |
| } | |
| override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ | |
| let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell | |
| let boolForSec = boolArray[indexPath.section] as String | |
| if (boolForSec.toInt() != 0) { | |
| var arr : [String] = dataDic[groupArray[indexPath.section]]! | |
| cell.textLabel?.text = arr[indexPath.row] as String | |
| }else { | |
| } | |
| // cell.textLabel?.text = "row \(indexPath.row)" | |
| return cell | |
| } | |
| override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { | |
| return 50 | |
| } | |
| override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { | |
| return 1 | |
| } | |
| override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { | |
| let headerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40)) | |
| headerView.backgroundColor = UIColor.blueColor() | |
| headerView.tag = section | |
| let headerString = UILabel(frame: CGRect(x: 10, y: 10, width: tableView.frame.size.width-10, height: 30)) as UILabel | |
| headerString.text = groupArray[section] as String | |
| headerView .addSubview(headerString) | |
| let headerTapped = UITapGestureRecognizer (target: self, action:"sectionHeaderTapped:") | |
| headerView .addGestureRecognizer(headerTapped) | |
| return headerView | |
| } | |
| func sectionHeaderTapped(tapped: UITapGestureRecognizer){ | |
| let section = tapped.view?.tag | |
| let boolForSec = boolArray[section!] as String | |
| if (boolForSec.toInt() != 0) { | |
| boolArray[section!] = "0" | |
| }else { | |
| boolArray[section!] = "1" | |
| } | |
| tableView.reloadSections(NSIndexSet(index: section!), withRowAnimation: UITableViewRowAnimation.Fade) | |
| } | |
| /* | |
| // MARK: - Navigation | |
| // In a storyboard-based application, you will often want to do a little preparation before navigation | |
| override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { | |
| // Get the new view controller using [segue destinationViewController]. | |
| // Pass the selected object to the new view controller. | |
| } | |
| */ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment