thieumao

Edit Continue

Mar 27th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 7.97 KB | None | 0 0
  1. import UIKit
  2.  
  3. let defaultTriggerHeight: CGFloat = 52.0
  4. let defaultTriggerHeightHasData: CGFloat = 86.0
  5.  
  6. enum EventDetailType: Int {
  7.     case SetTime
  8.     case CheckBoxList
  9.     case GoogleCalendar
  10. }
  11.  
  12. struct EventDetail {
  13.     var type: EventDetailType = .GoogleCalendar
  14.     var height: CGFloat = defaultTriggerHeight // default no has data
  15.     var isExpanding: Bool = false
  16.     var hasData: Bool = false
  17. }
  18.  
  19. struct Section {
  20.     var eventDetailList: [EventDetail] = []
  21. }
  22.  
  23. class EventDetailSettingViewController: BaseViewController {
  24.    
  25.     var sectionList: [Section] = []
  26.    
  27.     let numberOfFixedSection = 2
  28.     let topSection = 0
  29.     var numberOfEventDetailSection = 0
  30.     var bottomSection = 1
  31.     let heightEventDetailHeader: CGFloat = 54
  32.     let defaultNumberOfRow = 1
  33.    
  34.     @IBOutlet weak var eventDetailTableView: UITableView!
  35.    
  36.     override func viewDidLoad() {
  37.         super.viewDidLoad()
  38.         self.addCustomBackButton()
  39.         navigationItem.title = "EVENT_DETAIL_SETTING".localized
  40.         registerCellClass()
  41.         callAPI()
  42.     }
  43.    
  44.     func registerCellClass() {
  45.         eventDetailTableView.register(UINib(nibName: "EventDetailHeaderView", bundle: nil),
  46.             forHeaderFooterViewReuseIdentifier: EventDetailHeaderView.identifier)
  47.         eventDetailTableView.register(UINib(nibName: "EventDetailSettingHeaderTableViewCell", bundle: nil),
  48.             forCellReuseIdentifier: EventDetailSettingHeaderTableViewCell.identifier)
  49.         eventDetailTableView.register(UINib(nibName: "EventDetailSettingSaveButtonTableViewCell", bundle: nil),
  50.             forCellReuseIdentifier: EventDetailSettingSaveButtonTableViewCell.identifier)
  51.         eventDetailTableView.register(UINib(nibName: "BoccoTableViewCell", bundle: nil),
  52.             forCellReuseIdentifier: BoccoTableViewCell.identifier)
  53.         eventDetailTableView.register(UINib(nibName: "EventDetailSettingSetTimeTableViewCell", bundle: nil),
  54.             forCellReuseIdentifier: EventDetailSettingSetTimeTableViewCell.identifier)
  55.         eventDetailTableView.register(UINib(nibName: "GoogleCalendarTableViewCell", bundle: nil),
  56.             forCellReuseIdentifier: GoogleCalendarTableViewCell.identifier)
  57.     }
  58.    
  59.     func callAPI() {
  60.         // TODO: This value will change when call API
  61.        
  62.         // This is data for test UI
  63.         let eventDetail = EventDetail()
  64.         var eventDetailList: [EventDetail] = []
  65.         eventDetailList.append(eventDetail)
  66.         eventDetailList.append(eventDetail)
  67.         let section = Section(eventDetailList: eventDetailList)
  68.         self.sectionList.append(section)
  69.        
  70.         let eventDetail2 = EventDetail()
  71.         var eventDetailList2: [EventDetail] = []
  72.         eventDetailList2.append(eventDetail2)
  73.         let section2 = Section(eventDetailList: eventDetailList2)
  74.         self.sectionList.append(section2)
  75.         // end data for test UI
  76.        
  77.         numberOfEventDetailSection = sectionList.count
  78.         bottomSection = numberOfEventDetailSection + 1
  79.     }
  80.    
  81.     fileprivate func isRange(_ startIndex: Int, _ endIndex: Int, containsValue value: Int) -> Bool {
  82.         return (startIndex + 1)..<endIndex ~= value
  83.     }
  84.  
  85. }
  86.  
  87. extension EventDetailSettingViewController: UITableViewDataSource {
  88.    
  89.     func numberOfSections(in tableView: UITableView) -> Int {
  90.         return numberOfFixedSection + numberOfEventDetailSection
  91.     }
  92.    
  93.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  94.         if self.isRange(topSection, bottomSection, containsValue: section) {
  95.             let position = section - 1
  96.             return self.sectionList[position].eventDetailList.count
  97.         }
  98.         return defaultNumberOfRow
  99.     }
  100.    
  101.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  102.         switch indexPath.section {
  103.         case topSection:
  104.             if let cell = tableView.dequeueReusableCell(withIdentifier:
  105.                 EventDetailSettingHeaderTableViewCell.identifier, for: indexPath) as?
  106.                 EventDetailSettingHeaderTableViewCell {
  107.                     return cell
  108.             }
  109.         case bottomSection:
  110.             if let cell = tableView.dequeueReusableCell(withIdentifier:
  111.                 EventDetailSettingSaveButtonTableViewCell.identifier, for: indexPath) as?
  112.                 EventDetailSettingSaveButtonTableViewCell {
  113.                     return cell
  114.             }
  115.         default:
  116.             let positionSection = indexPath.section - 1
  117.             let positionEventDetail = indexPath.row
  118.            
  119.             let eventDetailList = self.sectionList[positionSection].eventDetailList
  120.             let eventDetail = eventDetailList[positionEventDetail]
  121.            
  122.             if eventDetail.type == .GoogleCalendar, let cell = tableView.dequeueReusableCell(withIdentifier:
  123.                 GoogleCalendarTableViewCell.identifier, for: indexPath) as? GoogleCalendarTableViewCell {
  124.                
  125.                 cell.hasData = eventDetail.hasData
  126.                 cell.isExpanding = eventDetail.isExpanding
  127.                
  128.                 cell.expandView = { (hasData, height) in
  129.                     let positionSection = indexPath.section - 1
  130.                     let positionEventDetail = indexPath.row
  131.                     let eventDetailList = self.sectionList[positionSection].eventDetailList
  132.                     let eventDetail = eventDetailList[positionEventDetail]
  133.                     let heightOfHeaderView = eventDetail.hasData ? defaultTriggerHeightHasData :
  134.                         defaultTriggerHeight
  135.                     let heightOfDetailView = eventDetail.isExpanding ? 0 : height
  136.                    
  137.                     self.sectionList[positionSection].eventDetailList[positionEventDetail].height =
  138.                         heightOfHeaderView + heightOfDetailView
  139.                     self.sectionList[positionSection].eventDetailList[positionEventDetail].hasData = hasData
  140.                     self.sectionList[positionSection].eventDetailList[positionEventDetail].isExpanding =
  141.                         !eventDetail.isExpanding
  142.                     tableView.reloadRows(at: [indexPath], with: .fade)
  143.                 }
  144.                
  145.                 return cell
  146.             }
  147.         }
  148.         return UITableViewCell()
  149.     }
  150.    
  151. }
  152.  
  153. extension EventDetailSettingViewController: UITableViewDelegate {
  154.    
  155.     func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  156.         if self.isRange(topSection, bottomSection, containsValue: section) {
  157.             return heightEventDetailHeader
  158.         }
  159.         return kZeroFloat
  160.     }
  161.    
  162.     func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  163.         return kZeroFloat
  164.     }
  165.    
  166.     func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  167.         return UITableViewAutomaticDimension
  168.     }
  169.  
  170.     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  171.         let section = indexPath.section
  172.         if self.isRange(topSection, bottomSection, containsValue: section) {
  173.             let positionSection = indexPath.section - 1
  174.             let positionEventDetail = indexPath.row
  175.             let eventDetailList = self.sectionList[positionSection].eventDetailList
  176.             let eventDetail = eventDetailList[positionEventDetail]
  177.             return eventDetail.height
  178.         }
  179.         return UITableViewAutomaticDimension
  180.     }
  181.    
  182.     func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  183.         guard
  184.             self.isRange(topSection, bottomSection, containsValue: section),
  185.             let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier:
  186.             EventDetailHeaderView.identifier) as? EventDetailHeaderView else {
  187.                 return UITableViewHeaderFooterView()
  188.         }
  189.         return headerView
  190.     }
  191.    
  192. }
Advertisement
Add Comment
Please, Sign In to add comment