欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

自定义带DropDownTable的TextField(事件)

发布时间:2025/7/14 54 豆豆
生活随笔 收集整理的这篇文章主要介绍了 自定义带DropDownTable的TextField(事件) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

上节已经把tableview成功的在uitextview的下面显示出来了。

现在需要给这个tableview添加点击DataSource和DataDelegate

先定义一个协议,用于外部ViewController传入数据和获取点击事件,函数作用顾名思义。

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代理函数中调用外部传入的该代理实例。

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中实现自定义的协议,并且将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和数据操作都完成了

 

转载于:https://www.cnblogs.com/mogul/p/5157699.html

总结

以上是生活随笔为你收集整理的自定义带DropDownTable的TextField(事件)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。