Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- enum EventDetailType : Int {
- case setTime
- case checkBoxList
- }
- struct EventDetail {
- var type: EventDetailType = .setTime
- var height: Int = 0
- var isExpanding: Bool = false
- var hasData: Bool = false
- }
- struct Section {
- var eventDetailList: [EventDetail] = []
- }
- class EventDetailSettingViewController: BaseViewController {
- var sectionList: [Section] = []
- let numberOfFixedSection = 2
- let topSection = 0
- var numberOfEventDetailSection = 0
- var bottomSection = 1
- let heightEventDetailHeader: CGFloat = 54
- let defaultNumberOfRow = 1
- let defaultTriggerHeight: CGFloat = 52.0
- var cellsHeight: [CGFloat] = []
- var isExpanding: [Bool] = []
- // TODO: Remove sample property when using SDK
- var schedule: Schedule!
- @IBOutlet weak var eventDetailTableView: UITableView!
- override func viewDidLoad() {
- super.viewDidLoad()
- self.addCustomBackButton()
- navigationItem.title = "EVENT_DETAIL_SETTING".localized
- registerCellClass()
- callAPI()
- // 7 days in a week, start at 2 -> Monday, end at Sunday -> 8
- self.schedule = Schedule(daysInWeek: [String](repeating: "", count: 9), time: "")
- self.cellsHeight = [CGFloat](repeating: self.defaultTriggerHeight, count: self.bottomSection + 1)
- self.isExpanding = [Bool](repeating: false, count: self.bottomSection + 1)
- }
- func registerCellClass() {
- eventDetailTableView.register(UINib(nibName: "EventDetailHeaderView", bundle: nil),
- forHeaderFooterViewReuseIdentifier: EventDetailHeaderView.identifier)
- eventDetailTableView.register(UINib(nibName: "EventDetailSettingHeaderTableViewCell", bundle: nil),
- forCellReuseIdentifier: EventDetailSettingHeaderTableViewCell.identifier)
- eventDetailTableView.register(UINib(nibName: "EventDetailSettingSaveButtonTableViewCell", bundle: nil),
- forCellReuseIdentifier: EventDetailSettingSaveButtonTableViewCell.identifier)
- eventDetailTableView.register(UINib(nibName: "BoccoTableViewCell", bundle: nil),
- forCellReuseIdentifier: BoccoTableViewCell.identifier)
- eventDetailTableView.register(UINib(nibName: "EventDetailSettingSetTimeTableViewCell", bundle: nil),
- forCellReuseIdentifier: EventDetailSettingSetTimeTableViewCell.identifier)
- }
- func callAPI() {
- // TODO: This value will change when call API
- let eventDetail = EventDetail()
- var eventDetailList: [EventDetail] = []
- eventDetailList.append(eventDetail)
- eventDetailList.append(eventDetail)
- let section = Section(eventDetailList: eventDetailList)
- sectionList.append(section)
- let eventDetail2 = EventDetail()
- var eventDetailList2: [EventDetail] = []
- eventDetailList2.append(eventDetail2)
- let section2 = Section(eventDetailList: eventDetailList2)
- sectionList.append(section2)
- numberOfEventDetailSection = sectionList.count
- bottomSection = numberOfEventDetailSection + 1
- }
- fileprivate func isRange(_ startIndex: Int, _ endIndex: Int, containsValue value: Int) -> Bool {
- return (startIndex + 1)..<endIndex ~= value
- }
- }
- extension EventDetailSettingViewController: UITableViewDataSource {
- func numberOfSections(in tableView: UITableView) -> Int {
- return numberOfFixedSection + numberOfEventDetailSection
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- if !self.isRange(topSection, bottomSection, containsValue: section) {
- return defaultNumberOfRow
- }
- // for index in 0..<self.sectionList.count {
- //
- // }
- let position = section - 1
- return self.sectionList[position].eventDetailList.count;
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- switch indexPath.section {
- case topSection:
- if let cell = tableView.dequeueReusableCell(withIdentifier:
- EventDetailSettingHeaderTableViewCell.identifier, for: indexPath) as?
- EventDetailSettingHeaderTableViewCell {
- return cell
- }
- case bottomSection:
- if let cell = tableView.dequeueReusableCell(withIdentifier:
- EventDetailSettingSaveButtonTableViewCell.identifier, for: indexPath) as?
- EventDetailSettingSaveButtonTableViewCell {
- return cell
- }
- default:
- if let cell = tableView.dequeueReusableCell(withIdentifier:
- EventDetailSettingSetTimeTableViewCell.identifier, for: indexPath)
- as? EventDetailSettingSetTimeTableViewCell {
- cell.isExpanding = self.isExpanding[indexPath.section]
- cell.schedule = self.schedule
- cell.resizeView = { height in
- self.cellsHeight[indexPath.section] += height
- tableView.reloadRows(at: [indexPath], with: .fade)
- }
- cell.expandView = { height in
- if self.isExpanding[indexPath.section] {
- self.cellsHeight[indexPath.section] -= height
- } else {
- self.cellsHeight[indexPath.section] += height
- }
- self.isExpanding[indexPath.section] = !self.isExpanding[indexPath.section]
- tableView.reloadRows(at: [indexPath], with: .fade)
- }
- return cell
- }
- }
- return UITableViewCell()
- }
- }
- extension EventDetailSettingViewController: UITableViewDelegate {
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- if self.isRange(topSection, bottomSection, containsValue: section) {
- return heightEventDetailHeader
- }
- return kZeroFloat
- }
- func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
- return kZeroFloat
- }
- func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
- return UITableViewAutomaticDimension
- }
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- let section = indexPath.section
- if self.isRange(topSection, bottomSection, containsValue: section) {
- return self.cellsHeight[section]
- }
- return UITableViewAutomaticDimension
- }
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- guard
- self.isRange(topSection, bottomSection, containsValue: section),
- let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier:
- EventDetailHeaderView.identifier) as? EventDetailHeaderView else {
- return UITableViewHeaderFooterView()
- }
- return headerView
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement