Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. <UITableView>
  2. Default Section Header
  3. Row 1
  4. Row 2
  5. Row 3
  6. </UITableView>
  7.  
  8. <UITableView>
  9. Default Section Header
  10. Row 1
  11. Row 2
  12. Row 3
  13.  
  14. Newly added section header
  15. Row 1
  16. Row 2
  17. Row 3
  18. Row 4
  19. </UITableView>
  20.  
  21. class MyInfoView: UIView, UITableViewDataSource, UITableViewDelegate {
  22.  
  23. @IBOutlet weak var myInfoTableView: UITableView!
  24.  
  25. var headerNamesArray: [String]?//= ["Personal Details", "Contact Details","Emergency Contacts", "Dependents", "Immigration", "Job", "Salary", "Report-to", "Qualifications", "Memberships"]
  26. var cellDetailsArray:NSDictionary?
  27. var currentExpandedIndex:Int = -1
  28. var isButtonTapped:Bool = false
  29.  
  30. //MARK:- Register Tableview Method -
  31. func registerTableView()
  32. {
  33. println(self.frame)
  34. myInfoTableView.frame = CGRectMake(0.0, 0.0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) - 64)
  35. myInfoTableView.registerNib(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "InfoCellID")
  36. loadTableViewDetails()
  37. }
  38. //MARK:- Loading details from Plist -
  39. func loadTableViewDetails()
  40. {
  41. cellDetailsArray = NSDictionary(contentsOfFile: NSBundle.mainBundle().pathForResource("TextFieldNames", ofType: "plist")!)
  42. headerNamesArray = cellDetailsArray!.allKeys as? [String]
  43. println(cellDetailsArray)
  44. }
  45. //MARK:- tableview header button Action -
  46. func headerButtonTapped(sender: AnyObject)
  47. {
  48. isButtonTapped = !isButtonTapped
  49.  
  50. var anIndex:NSInteger = sender.tag
  51. var indexSet:NSMutableIndexSet = NSMutableIndexSet(index: anIndex)
  52.  
  53. if anIndex == currentExpandedIndex {
  54. // myInfoTableView.deleteRowsAtIndexPaths( (currentExpandedIndex, withRowAnimation: UITableViewRowAnimation.Right);
  55. currentExpandedIndex = -1
  56. } else if (currentExpandedIndex != -1 ) {
  57. indexSet.addIndex(currentExpandedIndex)
  58. currentExpandedIndex = anIndex
  59. isButtonTapped = true
  60. } else {
  61. currentExpandedIndex = anIndex
  62. }
  63. println(indexSet)
  64. myInfoTableView.reloadSections(indexSet ,withRowAnimation:UITableViewRowAnimation.Middle)
  65. }
  66. }
  67. //MARK:- TableView Delegate and DataSource Methods -
  68. extension MyInfoView: UITableViewDataSource, UITableViewDelegate {
  69.  
  70. func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  71. return headerNamesArray!.count
  72. }
  73.  
  74. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  75. cellDetailsArray![headerNamesArray![section]];
  76. return cellDetailsArray![headerNamesArray![section]]!.count;
  77. }
  78.  
  79. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  80. let cell = tableView.dequeueReusableCellWithIdentifier("InfoCellID", forIndexPath: indexPath) as! CustomTableViewCell
  81. var headerSection = indexPath.section
  82. cell.userTextField.placeholder = cellDetailsArray![headerNamesArray![headerSection]]![indexPath.row] as? String
  83.  
  84. return cell
  85. }
  86.  
  87. func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
  88. if isButtonTapped && currentExpandedIndex == indexPath.section {
  89. return 40.0
  90. }
  91. else {
  92. return 0.0
  93. }
  94. }
  95. func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  96. return 0.5
  97. }
  98.  
  99. func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  100. let headerView = NSBundle.mainBundle().loadNibNamed("TableHeaderView", owner: self, options: nil) [0] as! TableHeaderView
  101. headerView.headerButton.setTitle(headerNamesArray![section], forState: .Normal)
  102. headerView.applyViewStyle()
  103. headerView.headerButton.tag = section
  104. headerView.headerButton.addTarget(self, action: Selector("headerButtonTapped:"), forControlEvents:.TouchUpInside)
  105. headerView.arrowImageView.image = UIImage(named: "sidearrow.png")
  106. if currentExpandedIndex == section {
  107. headerView.arrowImageView.image = UIImage(named: "downarrow.png")
  108. }
  109. return headerView
  110. }
  111. func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  112. return 40.0
  113. }
  114. func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
  115. return true
  116. }
  117. func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
  118.  
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement