Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. func selectedSection(sender: UIButton) {
  2. let previousSelected = self.selectedSection
  3. self.selectedSection = sender.tag
  4.  
  5. if previousSelected != self.selectedSection {
  6. // one section opens another gets closed
  7. // workaround so the animation does not look awful for section headers
  8. print("Previous: (previousSelected)")
  9. sectionHeaders[previousSelected].toggleIcon()
  10. print("Selected: (self.selectedSection)")
  11. sectionHeaders[self.selectedSection].toggleIcon()
  12. print("***********")
  13.  
  14. var indexPathToInsert = [IndexPath]()
  15. var indexPathToDelete = [IndexPath]()
  16.  
  17. for i in 0..<self.menuItems[self.selectedSection].getSubItems().count {
  18. indexPathToInsert.append(IndexPath(row: i, section: self.selectedSection))
  19. }
  20.  
  21. for i in 0..<self.menuItems[previousSelected].getSubItems().count {
  22. indexPathToDelete.append(IndexPath(row: i, section: previousSelected))
  23. }
  24.  
  25.  
  26. tableView.beginUpdates()
  27. tableView.deleteRows(at: indexPathToDelete, with: .none)
  28. tableView.insertRows(at: indexPathToInsert, with: .automatic)
  29. tableView.endUpdates()
  30. } else {
  31. // close the section so the selected section is 0 again
  32. self.selectedSection = 0
  33.  
  34. print("Previous: (previousSelected)")
  35. sectionHeaders[previousSelected].toggleIcon()
  36. print("***********")
  37. tableView.reloadSections([previousSelected], with: .none)
  38. }
  39. }
  40.  
  41. func toggleIcon() {
  42. isOpen = !isOpen
  43. print("(isOpen)")
  44.  
  45. if isOpen {
  46. print("Works")
  47. self.sectionIcon.image = UIImage(named: "arrow-up")
  48. } else {
  49. print("Does not work")
  50. self.sectionIcon.image = UIImage(named: "arrow-down")
  51. }
  52. }
  53.  
  54. 'Selected: 1'
  55. true
  56. Works
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement