自定义带DropDownTable的TextField(事件)
生活随笔
收集整理的這篇文章主要介紹了
自定义带DropDownTable的TextField(事件)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
上節(jié)已經(jīng)把tableview成功的在uitextview的下面顯示出來了。
現(xiàn)在需要給這個tableview添加點擊DataSource和DataDelegate
先定義一個協(xié)議,用于外部ViewController傳入數(shù)據(jù)和獲取點擊事件,函數(shù)作用顧名思義。
public protocol DropDownTextFiledDataSourceDelegate:NSObjectProtocol{func dropDownTextField(dropDownTextField:DropDownTextField,numberOfRowsInSection section:Int)->Intfunc dropDownTextField(dropDownTextField:DropDownTextField,cellForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCellfunc dropDownTextField(dropDownTextField:DropDownTextField,didSelectRowAtIndexPath indexPath: NSIndexPath) }在UITextField的tableview代理函數(shù)中調(diào)用外部傳入的該代理實例。
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 dataSourceDelegate.respondsToSelector(Selector("dropDownTextField:numberOfRowsInSection:")) {return dataSourceDelegate.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 (separators) and data source (accessory views, editing controls)@available(iOS 2.0, *)public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{if let dataSourceDelegate = dataSourceDelegate {if dataSourceDelegate.respondsToSelector(Selector("dropDownTextField:cellForRowAtIndexPath:")) {return dataSourceDelegate.dropDownTextField(self, cellForRowAtIndexPath: indexPath)}}return UITableViewCell()}public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){if let dataSourceDelegate = dataSourceDelegate {if dataSourceDelegate.respondsToSelector(Selector("dropDownTextField:didSelectRowAtIndexPath:")) {dataSourceDelegate.dropDownTextField(self, didSelectRowAtIndexPath: indexPath)}}? ? ? ? ? ? ? self.dropTable.hidden = true?
}}
在外部ViewController中實現(xiàn)自定義的協(xié)議,并且將ViewController自身傳遞給DropDownTextFiled。
dropTextField.dataSourceDelegate = self extension 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? = dropDownTextField.dropTable.dequeueReusableCellWithIdentifier(reuseIdentifier)if cell == nil {cell = UITableViewCell(style: .Default, reuseIdentifier: reuseIdentifier)}cell!.textLabel!.text = "hello"cell!.textLabel?.numberOfLines = 0return cell!}func dropDownTextField(dropdownTextField: DropDownTextField, didSelectRowAtIndexPath indexPath: NSIndexPath) {resultLabel.text = "\(indexPath.row)"} }至此,ui和數(shù)據(jù)操作都完成了
?
轉(zhuǎn)載于:https://www.cnblogs.com/mogul/p/5157699.html
總結(jié)
以上是生活随笔為你收集整理的自定义带DropDownTable的TextField(事件)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++大数模版
- 下一篇: 解决Tomcat启动时出现的The AP