Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- let defaultTriggerHeight: CGFloat = 52.0
- let defaultTriggerHeightHasData: CGFloat = 86.0
- enum EventDetailType: Int {
- case SetTime
- case CheckBoxList
- case GoogleCalendar
- }
- struct EventDetail {
- var type: EventDetailType = .GoogleCalendar
- var height: CGFloat = defaultTriggerHeight // default no has data
- 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
- @IBOutlet weak var eventDetailTableView: UITableView!
- override func viewDidLoad() {
- super.viewDidLoad()
- self.addCustomBackButton()
- navigationItem.title = "EVENT_DETAIL_SETTING".localized
- registerCellClass()
- callAPI()
- }
- 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)
- eventDetailTableView.register(UINib(nibName: "GoogleCalendarTableViewCell", bundle: nil),
- forCellReuseIdentifier: GoogleCalendarTableViewCell.identifier)
- }
- func callAPI() {
- // TODO: This value will change when call API
- // This is data for test UI
- let eventDetail = EventDetail()
- var eventDetailList: [EventDetail] = []
- eventDetailList.append(eventDetail)
- eventDetailList.append(eventDetail)
- let section = Section(eventDetailList: eventDetailList)
- self.sectionList.append(section)
- let eventDetail2 = EventDetail()
- var eventDetailList2: [EventDetail] = []
- eventDetailList2.append(eventDetail2)
- let section2 = Section(eventDetailList: eventDetailList2)
- self.sectionList.append(section2)
- // end data for test UI
- 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) {
- let position = section - 1
- return self.sectionList[position].eventDetailList.count
- }
- return defaultNumberOfRow
- }
- 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:
- let positionSection = indexPath.section - 1
- let positionEventDetail = indexPath.row
- let eventDetailList = self.sectionList[positionSection].eventDetailList
- let eventDetail = eventDetailList[positionEventDetail]
- if eventDetail.type == .GoogleCalendar, let cell = tableView.dequeueReusableCell(withIdentifier:
- GoogleCalendarTableViewCell.identifier, for: indexPath) as? GoogleCalendarTableViewCell {
- cell.hasData = eventDetail.hasData
- cell.isExpanding = eventDetail.isExpanding
- cell.expandView = { (hasData, height) in
- let positionSection = indexPath.section - 1
- let positionEventDetail = indexPath.row
- let eventDetailList = self.sectionList[positionSection].eventDetailList
- let eventDetail = eventDetailList[positionEventDetail]
- let heightOfHeaderView = eventDetail.hasData ? defaultTriggerHeightHasData :
- defaultTriggerHeight
- let heightOfDetailView = eventDetail.isExpanding ? 0 : height
- self.sectionList[positionSection].eventDetailList[positionEventDetail].height =
- heightOfHeaderView + heightOfDetailView
- self.sectionList[positionSection].eventDetailList[positionEventDetail].hasData = hasData
- self.sectionList[positionSection].eventDetailList[positionEventDetail].isExpanding =
- !eventDetail.isExpanding
- 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) {
- let positionSection = indexPath.section - 1
- let positionEventDetail = indexPath.row
- let eventDetailList = self.sectionList[positionSection].eventDetailList
- let eventDetail = eventDetailList[positionEventDetail]
- return eventDetail.height
- }
- 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