我制作了一个自定义UITextField
,其中显示了“dropdownMenu”。但是发生的事情是,如果我在textField
中写字,那么Cell的每个textfield
突然开始显示下拉列表。另外还有一个问题。我在textfield
中有一个stackView
。此stackView
位于CardView
(用于影子)。这个CardView
在Cell中。如何在单元格内添加此suggestionTable
,而不是使用super.super.super.super.addSuView(suggestionTable)
添加单元格
open class SuggestionTextField: UITextField, UITextFieldDelegate {
var identifier = "SuggestionCell"
var suggestionTable:UITableView!
var suggestionlist = ["First","Second","Third","Four","Fifth","Sixth","Seven","eight","Nine","Ten"]
var filterSuggestionlist = [String]()
var heightConstraint:NSLayoutConstraint!
var defaultShow = true {
didSet {
self.suggestionTable.reloadData()
}
}
convenience init() {
self.init(
frame: CGRect.zero)
self.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(filterSuggestions), name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(filterSuggestions), name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
func keyboardWillShow(_ notification: Notification) {
let keyboardFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
guard let frame = self.superview?.convert(self.frame, to: UIApplication.shared.keyWindow) else { return }
//guard let frame = self.superview?.convert(self.frame, to: nil) else { return }
let gap = (UIScreen.main.bounds.size.height - frame.origin.y - frame.height - keyboardFrame.height)
guard suggestionTable != nil else {
return
}
heightConstraint.constant = gap
}
func keyboardWillHide(_ notification: Notification) {
}
func filterSuggestions(){
self.setup()
if (self.text?.isEmpty)! {
self.defaultShow = true
return
}
filterSuggestionlist = suggestionlist.filter({ (suggestion) -> Bool in
return suggestion.contains(self.text!)
})
self.defaultShow = false
}
func setup(){
guard suggestionTable == nil else {
return
}
suggestionTable = UITableView()
suggestionTable.allowsSelection = true
suggestionTable.dataSource = self
suggestionTable.delegate = self
suggestionTable.estimatedRowHeight = 40
suggestionTable.rowHeight = UITableViewAutomaticDimension
// suggestionTable.layer.masksToBounds = false
// suggestionTable.clipsToBounds = false
suggestionTable.contentInset = UIEdgeInsets.zero
suggestionTable.separatorStyle = .none
suggestionTable.layer.cornerRadius = 3
suggestionTable.layer.shadowColor = UIColor.black.cgColor
suggestionTable.layer.shadowOffset = CGSize(width: 2, height: 3)
suggestionTable.layer.shadowOpacity = 0.5
suggestionTable.showsVerticalScrollIndicator = true
suggestionTable.translatesAutoresizingMaskIntoConstraints = false
suggestionTable.register(suggestionCell.self, forCellReuseIdentifier: identifier)
superview?.superview?.superview?.superview?.addSubview(suggestionTable)
superview?.superview?.superview?.superview?.bringSubview(toFront: suggestionTable)
setupConstrain()
}
func setupConstrain(){
let leftConstraint = NSLayoutConstraint(item: suggestionTable, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 0)
let rightConstraint = NSLayoutConstraint(item:
suggestionTable, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: 0)
let topConstraint = NSLayoutConstraint(item: suggestionTable, attribute: .top, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 2)
heightConstraint = NSLayoutConstraint(item: suggestionTable, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200)
NSLayoutConstraint.activate([leftConstraint, rightConstraint, topConstraint, heightConstraint])
}
func removeSuggestionView(){
guard suggestionTable != nil else {
return
}
suggestionTable.removeFromSuperview()
suggestionTable = nil
}
public func textFieldDidBeginEditing(_ textField: UITextField) {
self.setup()
}
public func textFieldDidEndEditing(_ textField: UITextField) {
removeSuggestionView()
}
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
_ = self.resignFirstResponder()
return true
}
}
extension SuggestionTextField : UITableViewDelegate ,UITableViewDataSource {
public func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return defaultShow ? suggestionlist.count : filterSuggestionlist.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! suggestionCell
cell.textLabel?.text = defaultShow ? suggestionlist[indexPath.row] : filterSuggestionlist[indexPath.row]
cell.textLabel?.textColor = UIColor.black
cell.textLabel?.textAlignment = .center
cell.textLabel?.adjustsFontSizeToFitWidth = true
return cell
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.text = defaultShow ? suggestionlist[indexPath.row] : filterSuggestionlist[indexPath.row]
if !self.defaultShow {
self.defaultShow = !self.defaultShow
}
tableView.deselectRow(at: indexPath, animated: true)
}
}
谢谢!
答案 0 :(得分:0)
我认为,你在textfield中设置resignFirstResponder()或设置了becomeFirstResponder()来控制其他文本字段的容易。