Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I entered the text in textfield to filter the array and show in drop down list. But after entered 2nd letter drop down is showing on it. #330

Open
KOSURUUDAYSAIKUMAR opened this issue Dec 27, 2023 · 0 comments

Comments

@KOSURUUDAYSAIKUMAR
Copy link

import UIKit

@objc protocol DateFormatDelegate: AnyObject {
@objc func dateFilteredCases(date: [String])
@objc func presentPopupViewController(_ viewController: UIViewController)

}
class DateCalendarTableViewCell: UITableViewCell {

@IBOutlet weak var dateFormat: UITextField!
@IBOutlet weak var setDateTextField: UITextField!
@IBOutlet weak var dateLabel: UILabel!

@IBOutlet weak var calendarTextField: UITextField!
@IBOutlet weak var restrictMonth: UITextField!
@IBOutlet weak var previousCalendarDate: UITextField!

weak var dateDelegate: DateFormatDelegate?
let centeredDropDown = DropDown()
lazy var dropDowns: [DropDown] = {
    return [centeredDropDown]
}()
var filteredCases = [String]()

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    dateLabel.text = "Eg: dd-MM-YY, dd-MM-YYYY, MM-dd-YYYY, yyyy-MM-dd, YYYY-MM-dd HH:mm, YYYY-MM-dd HH:mm:ss"
    dateFormat.delegate = self
    dropDowns.forEach { $0.dismissMode = .onTap }
    dropDowns.forEach { $0.direction = .bottom }
}

func setupUI() {
  
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    // Configure the view for the selected state
}

}

extension DateCalendarTableViewCell: UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
// filteredCases = ["dd MMM yyyy HH:mm zzz", "dd MMM yyyy HH:mm", "dd-MM-yy", "dd-MM-yyyy"]
return true
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField == dateFormat {
        if let text = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) {
            filteredCases = DateFormate.filterCases(startingWith: text, caseSensitive: true)
            print("Filtered cases: \(filteredCases)")
            dateDelegate?.dateFilteredCases(date: filteredCases)
            DispatchQueue.main.async(group: .none, qos: .background, execute: { [self] in
                centeredDropDown.show()
                dropDownHandler()
            })
        }
    }
    return true
}

func dropDownHandler() {
    centeredDropDown.anchorView = dateFormat
    centeredDropDown.dataSource = filteredCases
    centeredDropDown.selectionAction = { [self] (index, item) in
        print("Item ", item)
        dateFormat.text = item
        setDateTextField.text = Date().convertToString(formate: item)
        centeredDropDown.hide()
    }
}

}

extension DateCalendarTableViewCell: calendarDateSelectDelegate {
func selectedDate(text: String, tag: Int) {
if tag == 0 {
calendarTextField.text = text
} else {
previousCalendarDate.text = text
}
}
}

extension DateCalendarTableViewCell: UIViewControllerTransitioningDelegate {

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant