customizing TextField with Dropdowntable (event)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.