The previous section has shown the success of TableView in the Uitextview below.
Now need to add click DataSource and datadelegate to this TableView
Define a protocol for external Viewcontroller incoming data and get click events, function as the name implies.
Public protocol dropdowntextfileddatasourcedelegate:nsobjectprotocol{ Func Dropdowntextfield ( Dropdowntextfield:dropdowntextfield,numberofrowsinsection section:int)->int func DropDownTextField ( Dropdowntextfield:dropdowntextfield,cellforrowatindexpath indexpath:nsindexpath)-UITableViewCell func Dropdowntextfield (Dropdowntextfield:dropdowntextfield,didselectrowatindexpath Indexpath:nsindexpath)}
The external incoming proxy instance is called in Uitextfield's TableView proxy function.
Public weak var datasourcedelegate:ztdropdowntextfielddatasourcedelegate?extension Dropdowntextfield: uitableviewdatasource,uitableviewdelegate{@available (IOS 2.0, *) public func TableView (Tableview:uitableview, Numberofrowsinsection section:int), int{if let datasourcedelegate = datasourcedelegate {if data Sourcedelegate.respondstoselector (Selector ("dropdowntextfield:numberofrowsinsection:")) {return DataSource Delegate.dropdowntextfield (self, Numberofrowsinsection:section)}} return 0}//Row Display. Implementers should *always* try to reuse cells by setting each cell's reuseidentifier and querying for available reusable Cells with Dequeuereusablecellwithidentifier://Cell gets various attributes set automatically based on table (Separa tors) and data source (accessory views, editing controls) @available (IOS 2.0, *) public func TableView (Tablevie W:uitableview, CellforrowatindexpaTh indexpath:nsindexpath)-UITableViewCell {if let datasourcedelegate = datasourcedelegate { If Datasourcedelegate.respondstoselector (Selector ("Dropdowntextfield:cellforrowatindexpath:")) {RET Urn Datasourcedelegate.dropdowntextfield (self, Cellforrowatindexpath:indexpath)}} return UITab Leviewcell ()} public func TableView (Tableview:uitableview, Didselectrowatindexpath indexpath:nsindexp ATH) {if let datasourcedelegate = datasourcedelegate {if datasourcedelegate.respondstoselector (Sele ctor ("Dropdowntextfield:didselectrowatindexpath:")) {Datasourcedelegate.dropdowntextfield (self, DidSelectR Owatindexpath:indexpath)}}
Self. Droptable. Hidden = true
} }
Implement the custom protocol in the external Viewcontroller, and pass the Viewcontroller itself to dropdowntextfiled.
Droptextfield.datasourcedelegate = selfextension Viewcontroller:dropdowntextfileddatasourcedelegate {func Dropdowntextfield (Dropdowntextfield:dropdowntextfield, Numberofrowsinsection section:int), Int {return 5 } func Dropdowntextfield (Dropdowntextfield:dropdowntextfield, Cellforrowatindexpath Indexpath:nsindexpath), UITableViewCell {Let reuseidentifier = "Dropdowncell" var cell:uitableviewcell? = Dropdowntextfie Ld.dropTable.dequeueReusableCellWithIdentifier (reuseidentifier) if cell = = Nil {cell = UITableViewCell ( Style:. Default, Reuseidentifier:reuseidentifier)} cell!. textlabel!. Text = "Hello" cell!. Textlabel?. NumberOfLines = 0 return cell! } func Dropdowntextfield (Dropdowntextfield:dropdowntextfield, Didselectrowatindexpath Indexpath:nsindexpath) { Resultlabel.text = "\ (indexpath.row)}}"
At this point, the UI and data operations are complete
customizing TextField with Dropdowntable (event)