Skip to content

Commit

Permalink
Merge pull request #7 from tejuamirthi/teju/bug/6
Browse files Browse the repository at this point in the history
No default behaviour for selecting a row from dropdown
  • Loading branch information
tejuamirthi authored Oct 11, 2020
2 parents f8408e2 + 5f99729 commit 3cd407e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 36 deletions.
18 changes: 17 additions & 1 deletion CustomDropDown/Presenter/CustomDropDownPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public protocol CustomDropDownDataSource: class {
func overrideDropDownView() -> UIView?
func dropDownWidth() -> CGFloat?
func dropDownTag() -> Int
func selectedLabelTag() -> Int
func dropDownLeftRightPadding() -> UIEdgeInsets
func numberOfSections(in tableView: UITableView) -> Int?
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
Expand All @@ -34,6 +35,10 @@ public extension CustomDropDownDataSource where Self: NSObject {
return 999
}

func selectedLabelTag() -> Int {
return 9999
}

func dropDownLeftRightPadding() -> UIEdgeInsets {
return UIEdgeInsets(top: 10, left: 10, bottom: 0, right: 10)
}
Expand Down Expand Up @@ -64,7 +69,18 @@ public extension CustomDropDownDataSource where Self: NSObject {
}

public protocol CustomDropDownDelegate: class {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath, displayView: UIView, tag: Int, data: Any)
}

public extension CustomDropDownDelegate where Self: NSObject {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath, displayView: UIView, tag: Int, data: Any) {
guard let text = data as? String else {
return
}
if let label = displayView.viewWithTag(tag) as? UILabel {
label.text = text
}
}
}

//class DropDownItem: UIView {
Expand Down
13 changes: 8 additions & 5 deletions CustomDropDown/View/CustomDropDown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import UIKit
public class DropDownDisplayView: UIView {
open var title: UILabel!
open var button: UIButton!
init() {
init(tag: Int) {
super.init(frame: .zero)
title = UILabel()
button = UIButton()
self.addSubview(title)
self.addSubview(button)
title.tag = tag
title.addAnchors(top: self.topAnchor, bottom: self.bottomAnchor, left: self.leftAnchor, right: button.leftAnchor, padding: 8, widthConstraint: nil, heightConstraint: title.heightAnchor.constraint(greaterThanOrEqualToConstant: 32))
title.attributedText = NSAttributedString(string: "Select-hello", attributes: [NSAttributedString.Key.foregroundColor : UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)])
title.attributedText = NSAttributedString(string: "Select-hello", attributes: [NSAttributedString.Key.foregroundColor : UIColor.white, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)])
button.addAnchors(top: self.topAnchor, bottom: self.bottomAnchor, left: title.rightAnchor, right: self.rightAnchor, padding: 8, widthConstraint: button.widthAnchor.constraint(equalToConstant: 60), heightConstraint: button.heightAnchor.constraint(equalToConstant: 32))
}

Expand Down Expand Up @@ -66,8 +67,9 @@ class CustomDropDownView<T>: UIView, UITableViewDataSource, UITableViewDelegate
guard let presenter = self.presenter else {
return
}
// self.dropDownDisplayView.title.attributedText = NSAttributedString(string: presenter.items[indexPath.row] as? String ?? "Hello", attributes: [NSAttributedString.Key.foregroundColor : UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)])
presenter.delegate?.tableView(tableView, didSelectRowAt: indexPath)
let tag = presenter.datasource?.selectedLabelTag() ?? 9999
presenter.delegate?.tableView(tableView, didSelectRowAt: indexPath, displayView: dropDownDisplayView, tag: tag, data: presenter.items[indexPath.row])
self.toggleDropDown()
}

func getStringLabelCell(indexPath: IndexPath) -> UITableViewCell {
Expand Down Expand Up @@ -100,7 +102,8 @@ class CustomDropDownView<T>: UIView, UITableViewDataSource, UITableViewDelegate
}

func setupDropDownDisplayView() {
dropDownDisplayView = DropDownDisplayView()
let tag = presenter?.datasource?.selectedLabelTag() ?? 9999
dropDownDisplayView = DropDownDisplayView(tag: tag)
}

func setupDropDown() {
Expand Down
30 changes: 0 additions & 30 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,6 @@ class ViewController: UIViewController {
//}

extension ViewController: CustomDropDownDelegate, CustomDropDownDataSource {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = UIView()
let label = UILabel()
label.attributedText = NSAttributedString(string: "Headersection", attributes: [NSAttributedString.Key.foregroundColor : UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)])
cell.backgroundColor = .lightGray
cell.addSubview(label)
label.addAnchors(top: cell.topAnchor, bottom: cell.bottomAnchor, left: cell.leftAnchor, right: cell.rightAnchor, padding: 8, widthConstraint: nil, heightConstraint: label.heightAnchor.constraint(greaterThanOrEqualToConstant: 32))
return cell
}

func overrideDropDownView() -> UIView? {
let customView = UIView()
let title = UILabel()
let button = UIImageView(image: UIImage(named: "arrow-down"))
customView.addSubview(title)
customView.addSubview(button)
title.addAnchors(top: customView.topAnchor, bottom: customView.bottomAnchor, left: customView.leftAnchor, right: button.leftAnchor, padding: 8, widthConstraint: nil, heightConstraint: title.heightAnchor.constraint(greaterThanOrEqualToConstant: 32))
title.attributedText = NSAttributedString(string: "My Custom hello", attributes: [NSAttributedString.Key.foregroundColor : UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)])
button.addAnchors(top: nil, bottom: nil, left: nil, right: customView.rightAnchor, padding: 16, widthConstraint: button.widthAnchor.constraint(equalToConstant: 32), heightConstraint: button.heightAnchor.constraint(equalToConstant: 32))
button.centerYAnchor.constraint(equalTo: customView.centerYAnchor).isActive = true
return customView
}
}

extension UIView {
Expand Down

0 comments on commit 3cd407e

Please sign in to comment.