ios - DropDownlist showing in every textfield of cell swift -
i made custom uitextfield
shows 'dropdownmenu'. happening if write in textfield
, every textfield
of cell starts showing dropdown. , there 1 other issue. have textfield
in stackview
. , stackview
in cardview
(used shadow). , cardview
in cell. how can add suggestiontable
inside cell instead of using super.super.super.super.addsuview(suggestiontable)
add in cell
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 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) } }
thanks!!
i think, set resignfirstresponder() in textfield or set becomefirstresponder() control easy @ textfield other.
wiki
Comments
Post a Comment