Advertisement
thieumao

Multi Section

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