Skip to content

Instantly share code, notes, and snippets.

@Abhishek9634
Last active April 3, 2019 04:40
Show Gist options
  • Save Abhishek9634/87f1b37ee49515809a37de83b3409618 to your computer and use it in GitHub Desktop.
Save Abhishek9634/87f1b37ee49515809a37de83b3409618 to your computer and use it in GitHub Desktop.
import UIKit
enum PerformanceDateGroup: Int {
case none = 0
case daily
case weekly
case monthly
init(rawValue: Int) {
switch rawValue {
case 0:
self = .daily
case 1:
self = .weekly
case 2:
self = .monthly
default:
self = .none
}
}
var value: String {
switch self {
case .daily:
return "Daily"
case .weekly:
return "Weekly"
case .monthly:
return "Monthly"
case .none:
return "None"
}
}
var headerTitle: String {
return "\(self.value) Usage"
}
}
protocol DropDownViewDelegate: class {
func didSelect(_ dropDown: DropDownView, model: DropDownCellModel)
}
class DropDownView: UIView {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var imgView: UIImageView!
@IBOutlet var dropDownHeightContraint: NSLayoutConstraint!
@IBOutlet weak var lineView: UIView!
@IBOutlet weak var titleView: UIView!
@IBOutlet weak var tableView: UITableView!
var cellItems: [DropDownCellModel] = []
weak var delegate: DropDownViewDelegate?
var isVisible: Bool {
return self.dropDownHeightContraint.constant == 120
}
override func awakeFromNib() {
super.awakeFromNib()
self.setupTableView()
self.setupGesture()
}
}
extension DropDownView: UITableViewDelegate, UITableViewDataSource {
private func setupGesture() {
let gesture = UITapGestureRecognizer(target: self,
action: #selector(handleGesture(_:)))
self.titleView.addGestureRecognizer(gesture)
}
@objc func handleGesture(_ sender: UITapGestureRecognizer) {
self.toggleMenu()
}
func toggleMenu() {
let constant = self.dropDownHeightContraint.constant
if constant == 30 {
self.dropDownHeightContraint.constant = 120
self.lineView.isHidden = false
} else {
self.dropDownHeightContraint.constant = 30
self.lineView.isHidden = true
}
UIView.animate(withDuration: 0.2) {
self.superview?.layoutIfNeeded()
}
}
private func setupTableView() {
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.register(DropDownTableViewCell.defaultNib,
forCellReuseIdentifier: "DropDownTableViewCell")
}
func setUpItems(_ items: [DropDownCellModel]) {
self.cellItems = items
self.tableView.reloadData()
}
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return self.cellItems.count
}
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(
withIdentifier: "DropDownTableViewCell"
) as! DropDownTableViewCell
cell.item = self.cellItems[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let rowHeight = tableView.bounds.height / CGFloat(self.cellItems.count)
return self.cellItems.isEmpty ? 0.0 : rowHeight
}
func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) {
self.handleFilterSelection(indexPath)
let model = self.cellItems[indexPath.row]
self.titleLabel.text = model.item.value
self.delegate?.didSelect(self, model: model)
self.toggleMenu()
}
private func handleFilterSelection(_ indexPath: IndexPath) {
self.cellItems.forEach { $0.isSelected = false }
self.tableView.reloadData()
self.cellItems[indexPath.row].isSelected = true
self.tableView.reloadRows(at: [indexPath], with: .automatic)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment